Cross-platform restored Form position and size lazarus / 31-LCL ---------------------------------------------------------------------- shidel · 2022-03-29 15:23 I'm having an couple issues regarding a cross-platform (x86_64 for macOS, Linux and Windows) that I have been working on. As the application continues to grow, the problem is becoming more and more tedious to deal with. I compile for each platform on that platform. I'm running Lazarus v2.3.0 on macOS, 2.20 on Windows and Linux. But, I don't think it is related to the version differences. Also, the program development files are kept in sync and each is built from the same code base. I do most of my work on the Mac. When I simply switch over to Linux and build, the program loads and saves it position like it should, splitters and such remember their positions, etc. But when I switch over to Windows, this is not the case, the program keeps going down/right and growing, splitters and such stay at IDE settings. However, if I open (and not sure if it necessary, I slightly move) each of the programs forms and save them. The version compiled for Windows will then also remember the settings like it should. But, I need to go through the process once again when I switch back to the Mac or Linux machines. Originally, I suspected XMLPropertyStorage. So, I modified everything to just use an XMLConfig. This made no difference. If I forget to jiggle and save a form, that form will load at it's default location and the restored settings for splitters and such are ignored. At present, there are a handful of forms and frames (6 or 7). But, this will be a fairly large application and it is only about 10% finished. The form jiggle and save is prone to errors. Are there any suggestions on removing the need for it or at least automating it? ---------------------------------------------------------------------- AlexTP · 2022-03-29 16:09 Try to save the window position into OS specific key under XML. You will have 3 different keys for Win Lin Mac. The OS name can be get from LowerCase({$I %FPCTARGETOS%}) Example code - https://github.com/Alexey-T/CudaText/blob/master/app/formabout.pas ---------------------------------------------------------------------- shidel · 2022-03-29 22:12 Thanks, but I don't see how that would help. Although the code base is shared. The save/restore settings are not. Those are not stored with the program. They are located in directories relevant to the platform and are not kept in sync between the build systems. Even deleting the saved state has no effect on behavior. When switching between Windows and mac/Linux, I need to open/save each form in the IDE. otherwise at runtime all such settings are not restored and are completely ignored. This includes Form Size and location, Splitter/Panel dimensions, etc. I have even had a log window running, that shows the file is read correctly and the settings it read were correct. But setting it, it gets ignored. Then I open, jiggle the form, save and compile. The forms the save and restore properly, the splitter goes where it is supposed to, etc. I guess, if there is an easy way to automate the open, jiggle and save, that would be good enough. But, I'll probably need to dig into the files generated and maintained by the IDE to figure out a way to "fix" it. ---------------------------------------------------------------------- dbannon · 2022-03-29 23:14 I am not quite sure what you are describing here but I suspect your problems might go away if you set these sizes / positions at run time, as the application starts up. Its a good idea anyway because you do not know in advance what  screen resolution your end user will have. Things like Screen.Width and Screen.height will tell you the screen size, calculate sensible values for the other things based on those numbers. Usually done in the form's OnCreate event. Davo ---------------------------------------------------------------------- shidel · 2022-03-30 00:49 That's the problem in a nutshell. When I switch from mac and/or Linux to Windows. The previous settings are read correctly from the file (which has settings based on monitor counts, resolutions, etc. to account for different preferences a single user may have when using a notebook or other various situations). That all works fine. And if the Form is moved it saves correctly as well. But, unless I open, jiggle and save the forms in the IDE when I switch from mac/linux to windows or from windows back to mac/linux. The settings that were saved and correctly read back... but are completely ignored. In other words, move Form, save settings, reopen program, read settings, set TOP, form does not move to position. Open, form in IDE move form slightly and save, recompile. Run program, reads settings, moves to previous position and size (like it should), move form, exit and reopen program, for moves to correct position. I only need to perform the open, jiggle, save process 1 time after switching to/from Windows to/from Linux/Mac. But, I need to do it every time I switch platforms. Like I said, the settings are stored and read back properly. But without the open, jiggle, save. They are ignored when attempting to apply them at runtime. ---------------------------------------------------------------------- dbannon · 2022-03-30 03:17 Nope, I do not understand ! Are you saving the settings in a standalone file or do you mean that you are relying on the IDE to save the settings for you ? When you do your "open, jiggle and save the forms in the IDE" do you then run the app within the IDE ?  Running, after changing anything, including form position, will recompile (as will changing platforms).  You cannot rely on the IDE to set position and sizes etc, if you want to save the position between runs of the app, it will have to be in a separate file. Thats often unnecessary because you can just calculate suitable positions at startup. If you are doing the separate file thing, and its not working, then you need to debug the save and reload cycle. Eventually, you will have a separate executable for each platform, they will have nothing to do with each other once compiled. Each one must be able to sort out where and how big it is by itself. Davo Davo ---------------------------------------------------------------------- shidel · 2022-03-30 11:35 Quote from: dbannon on March 30, 2022, 05:17:21 amAre you saving the settings in a standalone file or do you mean that you are relying on the IDE to save the settings for you ? At first, I just used TXMLPropStorage with a Filename configured. I suspected it could have been the cause. So, I switched everything over to manual storage and retrieval of the settings through TXMLConfig. This made no difference at all. The settings get saved when changes are made (checking the files). The Settings get loaded corrrectly (checking the simple Debugging TMemo based form in the application). The Settings get ignored by the LCL. Quote from: dbannon on March 30, 2022, 05:17:21 amWhen you do your "open, jiggle and save the forms in the IDE" do you then run the app within the IDE ?  Running, after changing anything, including form position, will recompile (as will changing platforms).  I have done both. Switch to the other platform. Compile and run using the IDE. And compile, shutdown IDE then Run. No difference. Open, jiggle and save 1 form. That form then works but the other forms do not. Open, jiggle and save all formes and frames (don't know if it is required for frames, it's just easier to do them all). Compile and run (inside or outside IDE) and everything works as it should. 0 lines of code changed. Quote from: dbannon on March 30, 2022, 05:17:21 amYou cannot rely on the IDE to set position and sizes etc, if you want to save the position between runs of the app, it will have to be in a separate file. Thats often unnecessary because you can just calculate suitable positions at startup. The app does save and load settings. It really does need to do that. Just calculating them at runtime wouldn't be acceptable for this type of application. Quote from: dbannon on March 30, 2022, 05:17:21 amIf you are doing the separate file thing, and its not working, then you need to debug the save and reload cycle. That has also been done. The settings get saved. The settings get loaded. Unless I open, jiggle and save a form in the IDE. Any attempt to set them is completely ignored. Not just the settings from the file. ANY settings. even hard coded, like SELF.HEIGHT=400. Once, switching platforms and I open, jiggle and save, such things work without issue. Quote from: dbannon on March 30, 2022, 05:17:21 amEventually, you will have a separate executable for each platform, they will have nothing to do with each other once compiled. Each one must be able to sort out where and how big it is by itself. Of course. They are also saved in different locations. Each executable for the different platforms works fine. However, I must open, jiggle and save every form inside the IDE for the application before compiling. Otherwise, all such size/position settings that get loaded (and logged) from file are ignored. If I am working on mac/Linux, everything is fine. I can work for hours, no problem save/load position/size settings. Switch over to windows, compile, forms (and other elements) load their settings and ignore them. Open, jiggle and save each form and frame in the project, compile and everything works fine. I can then spend a while tweaking and compiling in windows. Switch back to mac/linux and compile, once again the loaded size settings are ignored. I open, jiggle and save every form and frame in the IDE once again. Then compile and all is well. This only seems to apply to position/size settings with UI elements (Forms, Panels, etc). It does not happen with things like Caption, Checkboxs, etc. It's not a bug in the app. I don't even know if it is a bug in the LCL. My best guess to what is going on is as follows. On macOS, I'm using Lazarus 2.3 and it is my primary development platform. When I switch to Linux (Lazarus 2.2), the plaforms are similar enough that although there is a version difference in the IDEs, it loads and apply the form settings in a similar manner. However, on Windows (also 2.2), the platforms are very different and the lfm must be applied in a different order or some other minor technical difference which the IDE adjusts when I open, jiggle and save. I haven't tried switching from mac to linux. Then open, jiggle, save. Then switch to windows and see if the issue disappears if sticking to the same IDE version. What I'll probable do next to try and 'solve' the problem is update the IDE versions. I used to always run the latest version of Lazarus/FPC built directly from source. But since they switched from SVN to Git, that broke my automated build scripts. I guess I'll will rewrite those and go back to doing that. ---------------------------------------------------------------------- af0815 · 2022-03-30 11:50 I think this with the 'have to' jiggle is per design. If you open Lazarus and chnage nothing on the visual part, it should not change anything in the files. So you must do a 'action' to make a rewrite of the now changed files. Eg. new position and similar is now stored. I write prgs for Win, Linux and Raspbian. but the layout done witch anchors have no problem with the different platforms. ---------------------------------------------------------------------- shidel · 2022-03-30 12:17 Quote from: af0815 on March 30, 2022, 01:50:58 pmI think this with the 'have to' jiggle is per design. If you open Lazarus and chnage nothing on the visual part, it should not change anything in the files. So you must do a 'action' to make a rewrite of the now changed files. Eg. new position and similar is now stored. I write prgs for Win, Linux and Raspbian. but the layout done witch anchors have no problem with the different platforms. Same exact Lazarus version? Also, no Mac in the Mix? Do you cross- compile or build on the target system? The Jiggle part DOES change it slightly. It moves TOP/LEFT a few pixels. And save stores it. Without doing that, the runtime setting is ignored. Even if I do not load the setting and just HARD code the form position/size. And when I say hard code, I don't mean setting a Forms position property in the IDE. I mean Top:=10; Left:=10; Width:=640; Height:=300; is flat out ignored. If I open, jiggle and save them in the IDE. Then both loading and setting from the saved settings and hard coded are respected. ---------------------------------------------------------------------- af0815 · 2022-03-30 12:33 Normal i do a cross compilation from windows to the targetplatform. (but i have no biten fruits :-) ) I do hard coded in the first occouring OnActivate (for me a 'Init'). This is for me a point where i can all do, because all automatic task was running and i have the full control in the programm. I have in the past problems with D3-D7 if i do it to early, so i do it with fpc/lazarus also. ---------------------------------------------------------------------- Sieben · 2022-03-30 12:59 Does it also happen when you set Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Application.Scaled := False; in your project's .lpr...? I had a smilar problem here when projects designed in 96ppi were moved to a windows machine with 144ppi. Forms and eg grid column settings kept growing with every new start on that machine while working correctly on 96ppi systems. The above setting fixed it for now, but I reckon real cause and remedy is with the order of scaling and restoring settings but didn't dig deeper yet. ---------------------------------------------------------------------- shidel · 2022-03-30 14:39 Quote from: Sieben on March 30, 2022, 02:59:09 pmDoes it also happen when you set Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Application.Scaled := False; in your project's .lpr...? I had a smilar problem here when projects designed in 96ppi were moved to a windows machine with 144ppi. Forms and eg grid column settings kept growing with every new start on that machine while working correctly on 96ppi systems. The above setting fixed it for now, but I reckon real cause and remedy is with the order of scaling and restoring settings but didn't dig deeper yet. I just tried. If anything, seems the same. But, I suspect it is related. The Mac is running at 2.5K, Windows 4K and Linux at HD (1920x1080). ---------------------------------------------------------------------- wp · 2022-03-30 15:15 I suspect this is a consequence of LCL scaling: When you read the form size from the same cfg file accessible by multiple systems with different screen pixel densities(ppi) you must also store the pixels-per-inch related to the form/width and height. It is like storing distances in a file. Is the distance in meters, in kilometers, in miles or angstoms or light-years? With each unit you have different numbers. So, when you read the form size you must read the ppi together with the width and height values, and you must convert these values to the ppi of your current screen. Or, you convert the width/height to the standard 96ppi resolution before writing to the cfg file. Then after reading you convert these values back to the current resolution. ---------------------------------------------------------------------- Sieben · 2022-03-30 15:29 Quote from: wpOr, you convert the width/height to the standard 96ppi resolution before writing to the cfg file. Then after reading you convert these values back to the current resolution. Using eg this pair of methods from TControl...? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     function Scale96ToScreen(const ASize: Integer): Integer;    function ScaleScreenTo96(const ASize: Integer): Integer;  Or is there a better choice? ---------------------------------------------------------------------- dbannon · 2022-03-30 22:36 shidel, while agree that DPP must be involved, your real problem is that the form is not changing its size when it reads your config file on Windows. I suggest you concentrate on that and ignore (for now) the fact that jiggling in the IDE somehow affects the system. The DPP situation might mean that the size change is wrong, but it should still change. If that change is not happening, you need to investigate why not. Use the debugger to step though the process, add a button somewhere that alters the form size, manually edit the xml file to some, obviously different number. Once you understand what is happening (or not happening) the fix will be obvious. As far as I know LCL never ignores a size change, even a silly one but maybe negative numbers are a problem ? Other thoughts : There is a setting in the IDE, "maximise lfm compatibility" or something like that ensures lfm files made by trunk are readable by old versions of Lazarus but its unlikely anything has changed that recently.  Perhaps turn off HiDPI scaling ? Davo ---------------------------------------------------------------------- winni · 2022-03-30 23:26 Quote from: dbannon on March 31, 2022, 12:36:00 am Other thoughts : There is a setting in the IDE, "maximise lfm compatibility" or something like that ensures lfm files made by trunk are readable by old versions of Lazarus but its unlikely anything has changed that recently.  Hi! Project options --> Miscellaneous -->  Maximize compatibility of prohect files Winni ---------------------------------------------------------------------- shidel · 2022-03-31 00:24 Quote from: wp on March 30, 2022, 05:15:57 pmI suspect this is a consequence of LCL scaling: When you read the form size from the same cfg file accessible by multiple systems with different screen pixel densities(ppi) you must also store the pixels-per-inch related to the form/width and height. It is like storing distances in a file. Is the distance in meters, in kilometers, in miles or angstoms or light-years? With each unit you have different numbers. So, when you read the form size you must read the ppi together with the width and height values, and you must convert these values to the ppi of your current screen. Or, you convert the width/height to the standard 96ppi resolution before writing to the cfg file. Then after reading you convert these values back to the current resolution. The external config files are not shared across systems. Also, the application generates a unique ID based on what monitors are available and their resolutions. Settings are stored and retrieved under that ID's relative key in the settings file. That way if the resolution or monitor count changes the application will first display windows in a reasonable location. THIS WORKS. The settings are read back correctly and displayed in the log. But, your suggestion and the one from Sieben gives me an idea.......... Progress of a sort. Plus using lazbuild from command line (not the IDE) almost fixes it. Settings are restored correctly. I can see it open to the correct settings, a slight hesitation (1/4 of a second or less) then the application grows (so do splitters and such elements). Then using Application.Scaled:=False in the project almost fixes it. Forms restore to the proper location and size. Elements like Splitter move to the previous location. However, the autosizing of Labels, buttons doesn't work correctly. The text is larger than the area allotted. It also causes some elements (like labels) to appear to overlap (because size is to small). Side note... This requires me to do a quick build from the command line when switching back. Otherwise changes to the project file made by lazbuild cause the IDE to complain about unsupported targets, widget sets, etc.