alternetsoft / AlternetUI

MIT License
22 stars 2 forks source link

Windows can't remember thier old position #104

Closed Shadowblitz16 closed 6 months ago

Shadowblitz16 commented 6 months ago

I have been messing with this for a while to try to get the tool window to remember it's old location, I tried several events like OnActivate, OnClosing, OnLocationChanged, and OnVisabilityChanged.

Sadly even with StartPosition set to manual the window can't be set to remember it's old position.

using System;
using SMBXR.Engine;
using Alternet.UI;
using Alternet.Drawing;

namespace SMBXR;

public partial class LevelSettingsView : Window 
{
    public MainView? MainView { get; set; }
    public PointD   LastLocation { get; private set; }
    public LevelSettingsView()
    {
        IsToolWindow  = true;
        CloseEnabled  = false;
        StartLocation = WindowStartLocation.Manual;
        InitializeComponent();
    }

    private void OnSectionBoundsEdit(object sender, EventArgs args)
    {
        if (MainView == null) return;
        MainView.ResizeMode = SectionResizeMode.Edit;
    }

    private void OnSectionBoundsTop(object sender, EventArgs args)
    {
        if (MainView == null) return;
        MainView.ResizeMode = SectionResizeMode.Top;
    }

    private void OnSectionBoundsLeft(object sender, EventArgs args)
    {
        if (MainView == null) return;
        MainView.ResizeMode = SectionResizeMode.Left;
    }
    private void OnSectionBoundsRight(object sender, EventArgs args)
    {
        if (MainView == null) return;
        MainView.ResizeMode = SectionResizeMode.Right;
    }
    private void OnSectionBoundsBottom(object sender, EventArgs args)
    {
        if (MainView == null) return;
        MainView.ResizeMode = SectionResizeMode.Bottom;
    }

    protected override void OnLocationChanged(EventArgs e)
    {
        base.OnLocationChanged(e);
        if (Location == PointD.Empty) return;
        LastLocation = Location;
        Console.WriteLine("Moved To: "+LastLocation);
    }

    protected override void OnVisibleChanged(EventArgs e)
    {
        base.OnVisibleChanged(e);
        if (LastLocation == PointD.Empty) return;
        Location = LastLocation;
        Console.WriteLine("Reset To: "+Location);
    }
}
<Window xmlns="http://schemas.alternetsoft.com/ui/2021"
        xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml"
        x:Class="SMBXR.LevelSettingsView"
        xmlns:local="clr-namespace:SMBXR"
        Title="SMBXR Level Settings">
    <VerticalStackPanel>
        <HorizontalStackPanel>
            <GroupBox Text="Section Mode">
                <VerticalStackPanel>
                    <Button Name="buttonSectionEdit" Text="Edit" Click="OnSectionBoundsEdit"/>
                    <Button Name="buttonSectionTop" Text="Top" Click="OnSectionBoundsTop"/>
                    <Button Name="buttonSectionLeft" Text="Left"  Click="OnSectionBoundsLeft"/>
                    <Button Name="buttonSectionRight" Text="Right" Click="OnSectionBoundsRight"/>
                    <Button Name="buttonSectionBottom" Text="Bottom"  Click="OnSectionBoundsBottom"/>
                </VerticalStackPanel>
            </GroupBox>
        </HorizontalStackPanel>
    </VerticalStackPanel>
</Window>

using System;
using SMBXR.Engine;
using Alternet.UI;
using Alternet.Drawing;

namespace SMBXR;

public enum SectionResizeMode
{
    Edit,
    Top, 
    Left,
    Right,
    Bottom
}
public partial class MainView : Window 
{
    public Level Level { get; } = new();

    public PointD LastToolWindowPos { get; private set; }
    public SectionResizeMode ResizeMode { get; set; }

    public MainView()
    {
        InitializeComponent();

        var element = FindElement("LevelSettings");
        if (element is LevelSettingsView window)
        {
            window.MainView = this;
        } 
    }

    private void OnSave(object sender, EventArgs args)
    {

    }
    private void OnOpen(object sender, EventArgs args)
    {

    }
    private void OnExit(object sender, EventArgs args)
    {

    }

    private void OnLevelPaint(object sender, PaintEventArgs args)
    {
        args.DrawingContext.FillRectangle(new SolidBrush(Color.Black), args.Bounds);
    }
    private void OnLevelSelect(object sender, EventArgs args)
    {

    }
    private void OnLevelEraser(object sender, EventArgs args)
    {

    }
    private void OnLevelSettings(object sender, EventArgs args)
    {
        var element = FindElement("LevelSettings");
        if (element is Window window)
        {
            window.Visible = !window.Visible;
        }
    }
    private void OnLevelMouseDown(object sender, MouseEventArgs args)
    {

    }
}
<Window xmlns="http://schemas.alternetsoft.com/ui/2021"
        xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml"
        x:Class="SMBXR.MainView"
        xmlns:local="clr-namespace:SMBXR"
        Title="SMBXR Editor">
    <Window.Menu>
        <MainMenu>
            <MenuItem Text="_File">
                <MenuItem Text="_Open..." Name="openMenuItem" Click="OnOpen" Shortcut="Ctrl+O"/>
                <MenuItem Text="_Save..." Name="saveMenuItem" Click="OnSave" Shortcut="Ctrl+S"/>
                <MenuItem Text="-" Name="separatorMenuItem" />
                <MenuItem Text="E_xit" Name="exitMenuItem" Click="OnExit"/>
            </MenuItem>
        </MainMenu>
    </Window.Menu>
    <Panel Dock="Fill">
        <Panel Name="levelEdit" Dock="Fill" UserPaint="true" Paint="OnLevelPaint"/>
        <Panel Name="levelTools" Dock="Bottom" MouseDown="OnLevelMouseDown" VerticalAlignment="Bottom">
            <HorizontalStackPanel Dock="Fill">  
                <Button Text="Select" Name="selectButton" Click="OnLevelSelect"/>
                <Button Text="Eraser" Name="eraserButton" Click="OnLevelEraser"/>
                <Button Text="Settings" Name="settingsButton" Click="OnLevelSettings"/>
            </HorizontalStackPanel>
        </Panel>
    </Panel>

    <local:LevelSettingsView Name="LevelSettings"/>
</Window>
generalloki commented 6 months ago

Is this happens on Linux? If so, Window.Location is not possible to set on some Linux versions. For example, it happens on Ubuntu 23. This is due to new restrictions in Linux window manager and is not related to Alternet.UI. Currently I can suggest do not have separate windows in the applications if their locations are critical to the user interface. We cannot control window positions on Linux.

Shadowblitz16 commented 6 months ago

Is this happens on Linux? If so, Window.Location is not possible to set on some Linux versions. For example, it happens on Ubuntu 23. This is due to new restrictions in Linux window manager and is not related to Alternet.UI. Currently I can suggest do not have separate windows in the applications if their locations are critical to the user interface. We cannot control window positions on Linux.

that's too bad.