From d92e608746b030d4509f8073ad34ae131552a43f Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 18 Jun 2022 19:26:39 +0100 Subject: [PATCH] Maintain maximised state across restarts --- lapce-data/src/data.rs | 5 +++++ lapce-data/src/db.rs | 1 + lapce-ui/src/app.rs | 20 +++++++++++++++++--- lapce-ui/src/window.rs | 6 +++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index eba3de00..7b60f35f 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -109,6 +109,7 @@ pub fn load(event_sink: ExtEventSink) -> Self { let info = db.get_last_window_info().unwrap_or_else(|_| WindowInfo { size: Size::new(800.0, 600.0), pos: Point::new(0.0, 0.0), + maximised: false, tabs: TabsInfo { active_tab: 0, workspaces: vec![], @@ -208,6 +209,7 @@ pub struct LapceWindowData { pub watcher: Arc, /// The size of the window. pub size: Size, + pub maximised: bool, /// The position of the window. pub pos: Point, } @@ -218,6 +220,7 @@ fn same(&self, other: &Self) -> bool { && self.tabs.same(&other.tabs) && self.size.same(&other.size) && self.pos.same(&other.pos) + && self.maximised.same(&other.maximised) && self.keypress.same(&other.keypress) && self.plugins.same(&other.plugins) } @@ -311,6 +314,7 @@ pub fn new( watcher: Arc::new(watcher), size: info.size, pos: info.pos, + maximised: info.maximised, } } @@ -331,6 +335,7 @@ pub fn info(&self) -> WindowInfo { WindowInfo { size: self.size, pos: self.pos, + maximised: self.maximised, tabs: TabsInfo { active_tab, workspaces, diff --git a/lapce-data/src/db.rs b/lapce-data/src/db.rs index 2a1bb134..68634634 100644 --- a/lapce-data/src/db.rs +++ b/lapce-data/src/db.rs @@ -222,6 +222,7 @@ pub struct WorkspaceInfo { pub struct WindowInfo { pub size: Size, pub pos: Point, + pub maximised: bool, pub tabs: TabsInfo, } diff --git a/lapce-ui/src/app.rs b/lapce-ui/src/app.rs index bbf0716f..3e8cf7fd 100644 --- a/lapce-ui/src/app.rs +++ b/lapce-ui/src/app.rs @@ -1,6 +1,7 @@ use druid::{ AppDelegate, AppLauncher, Command, Env, Event, LocalizedString, Menu, MenuItem, Point, Size, SysMods, Widget, WidgetExt, WindowDesc, WindowHandle, WindowId, + WindowState, }; use lapce_data::{ command::{LapceUICommand, LAPCE_UI_COMMAND}, @@ -55,6 +56,7 @@ pub fn launch() { root, window_data.size, window_data.pos, + window_data.maximised, ); launcher = launcher.with_window(window); } @@ -68,6 +70,7 @@ fn new_window_desc( root: W, size: Size, pos: Point, + maximised: bool, ) -> WindowDesc where W: Widget + 'static, @@ -76,6 +79,9 @@ fn new_window_desc( .title(LocalizedString::new("Lapce").with_placeholder("Lapce")) .window_size(size) .set_position(pos); + if maximised { + desc = desc.set_window_state(WindowState::Maximized); + } if let Some(icon) = window_icon() { desc = desc.with_window_icon(icon); @@ -189,17 +195,19 @@ fn command( data: &mut LapceData, _env: &Env, ) -> druid::Handled { - if cmd.is(LAPCE_UI_COMMAND) { - let command = cmd.get_unchecked(LAPCE_UI_COMMAND); + if let Some(command) = cmd.get(LAPCE_UI_COMMAND) { if let LapceUICommand::NewWindow(from_window_id) = command { let (size, pos) = data .windows .get(from_window_id) + // If maximised, use default dimensions instead + .filter(|win| !win.maximised) .map(|win| (win.size, win.pos + (50.0, 50.0))) .unwrap_or((Size::new(800.0, 600.0), Point::new(0.0, 0.0))); let info = WindowInfo { size, pos, + maximised: false, tabs: TabsInfo { active_tab: 0, workspaces: vec![], @@ -214,7 +222,13 @@ fn command( let root = build_window(&window_data); let window_id = window_data.window_id; data.windows.insert(window_id, window_data); - let desc = new_window_desc(window_id, root, info.size, info.pos); + let desc = new_window_desc( + window_id, + root, + info.size, + info.pos, + info.maximised, + ); ctx.new_window(desc); return druid::Handled::Yes; } diff --git a/lapce-ui/src/window.rs b/lapce-ui/src/window.rs index 3749e440..23454959 100644 --- a/lapce-ui/src/window.rs +++ b/lapce-ui/src/window.rs @@ -3,7 +3,7 @@ widget::{LensWrap, WidgetExt}, BoxConstraints, Command, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Point, RenderContext, Size, Target, Widget, WidgetId, - WidgetPod, + WidgetPod, WindowState, }; use lapce_data::{ command::{LapceUICommand, LAPCE_UI_COMMAND}, @@ -183,6 +183,10 @@ fn event( Event::WindowSize(size) => { ctx.set_handled(); data.size = *size; + data.maximised = matches!( + ctx.window().get_window_state(), + WindowState::Maximized + ); } Event::WindowConnected => { ctx.submit_command(Command::new(