anakic/Jot

Closing app with minimized window

jakoss opened this issue · 6 comments

I have a strange problem. When i minimize window and then close the app - next time the app gets created is waaaay off the screen.

Configuration code:

public MainWindow()
{
     InitializeComponent();
     SourceInitialized += MainWindow_SourceInitialized;
}

private void MainWindow_SourceInitialized(object sender, EventArgs e)
{
      Persistance.Tracker.Configure(this).Apply();
 }

The settings file looks like that:

[
  {
    "Type": "System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
    "Name": "Height",
    "Value": 828.0
  },
  {
    "Type": "System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
    "Name": "Width",
    "Value": 1242.0
  },
  {
    "Type": "System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
    "Name": "Top",
    "Value": -32000.0
  },
  {
    "Type": "System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
    "Name": "Left",
    "Value": 0.0
  },
  {
    "Type": "System.Windows.WindowState, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
    "Name": "WindowState",
    "Value": 0
  }
]

+1 to this. I don't think anyone wants their app to start minimized, even if it was minimized when closed.

That said, I'm not sure how much Jot wants to add custom code to prevent these kinds of scenarios. Maybe a more generic approach (such as the ability to block/replace a particular enum value) would be preferable. I don't know the internals of Jot, but a potential solution could look like this.

The user could configure a function on the TrackingConfiguration<T> that performs a custom transformation on any value of the given type. In the case of this bug, I might register a transformation that handles any value of type WindowState, and returns the value Normal if the value is Minimized, otherwise leave the value as is.

// Set up the transformation when configuring the tracker so that we never save Minimized
configuration.Transform<WindowState>(ws => ws == WindowState.Minimized ? WindowState.Normal : ws);

// As defined by...
public TrackingConfiguration<T> Transform<TProp>(Func<TProp, TProp> transformPredicate) { ... }

For the second part of the bug, maybe we could define mins and maxes for certain values. We could use a similar transformation method, but instead of doing it for all values of a given type, we could pass a property name for the configuration engine to filter on (so it will only invoke our delegate if the value matches TProp and name). Since I don't want my window to start off screen, I can say that Left and Top should never be less than 0.

// Set up the transformation when configuring the tracker so that we never get a Top or Left value less than 0
configuration.Transform<double>(nameof(Window.Top), value => Math.Max(value, 0));
configuration.Transform<double>(nameof(Window.Left), value => Math.Max(value, 0));

// As defined by
public TrackingConfiguration<T> Transform<TProp>(string name, Func<TProp, TProp> transformPredicate) { ... }

Again, all of this is easy to say without actually diving into the code to see if it's possible. But hopefully it can spark some ideas! :-)

@jakoss I've just tried it and had no issues on both .net 4.7.2 and .net core 5. Can you make a sample project with the problem? Winforms do have this problem where the size/location is bogus while the window is minimized, but I haven't seen that behavior in WPF. For winForms you can handle it by hooking up a callback function by calling WhenPersistingProperty. See the example for winforms in the readme.

@micahmo Yep, that's already supported. There are methods you can use (WhenPersistingProperty and WhenApplyingProperty) to hook into the process of applying/persisting, and you can transform the value or cancel applying/persisting the property. Take a look at the winforms example in the readme, it cancels persisting if the windowstate is minimized.

Oh man, that's an ancient history for me. I don't even remember on what project i used it with :)

@anakic Thanks for the response! WhenPersistingProperty is exactly what I was looking for.

@jakoss yeah, my reply wasn't very quick:) if it pops up again, do reply back.

Can't reproduce it so closing for now. Will reopen if the issue pops up again.