Skip to content

Commit 118a749

Browse files
committed
Using windowRef for WindowDimension
1 parent 88252db commit 118a749

File tree

2 files changed

+71
-77
lines changed

2 files changed

+71
-77
lines changed

src/app.rs

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -100,52 +100,54 @@ live_design! {
100100
}
101101

102102
App = {{App}} {
103-
ui: <Window> {
104-
window: {inner_size: vec2(1280, 800), title: "Robrix"},
105-
caption_bar = {caption_label = {label = {text: "Robrix"}}}
106-
pass: {clear_color: #2A}
107-
108-
body = {
109-
// A wrapper view for showing top-level app modals/dialogs/popups
110-
<View> {
111-
width: Fill, height: Fill,
112-
flow: Overlay,
113-
114-
home_screen_view = <View> {
115-
visible: false
116-
home_screen = <HomeScreen> {}
117-
}
118-
login_screen_view = <View> {
119-
visible: true
120-
login_screen = <LoginScreen> {}
121-
}
122-
app_tooltip = <CalloutTooltip> {}
123-
popup = <PopupNotification> {
124-
margin: {top: 45, right: 13},
125-
content: {
126-
<PopupList> {}
103+
ui: <Root>{
104+
main_window = <Window> {
105+
window: {inner_size: vec2(1280, 800), title: "Robrix"},
106+
caption_bar = {caption_label = {label = {text: "Robrix"}}}
107+
pass: {clear_color: #2A}
108+
109+
body = {
110+
// A wrapper view for showing top-level app modals/dialogs/popups
111+
<View> {
112+
width: Fill, height: Fill,
113+
flow: Overlay,
114+
115+
home_screen_view = <View> {
116+
visible: false
117+
home_screen = <HomeScreen> {}
118+
}
119+
login_screen_view = <View> {
120+
visible: true
121+
login_screen = <LoginScreen> {}
122+
}
123+
app_tooltip = <CalloutTooltip> {}
124+
popup = <PopupNotification> {
125+
margin: {top: 45, right: 13},
126+
content: {
127+
<PopupList> {}
128+
}
127129
}
128-
}
129130

130-
// Context menus should be shown above other UI elements,
131-
// but beneath the verification modal.
132-
new_message_context_menu = <NewMessageContextMenu> { }
133-
134-
// message_source_modal = <Modal> {
135-
// content: {
136-
// message_source_modal_inner = <MessageSourceModal> {}
137-
// }
138-
// }
139-
140-
// We want the verification modal to always show up on top of
141-
// all other elements when an incoming verification request is received.
142-
verification_modal = <Modal> {
143-
content: {
144-
verification_modal_inner = <VerificationModal> {}
131+
// Context menus should be shown above other UI elements,
132+
// but beneath the verification modal.
133+
new_message_context_menu = <NewMessageContextMenu> { }
134+
135+
// message_source_modal = <Modal> {
136+
// content: {
137+
// message_source_modal_inner = <MessageSourceModal> {}
138+
// }
139+
// }
140+
141+
// We want the verification modal to always show up on top of
142+
// all other elements when an incoming verification request is received.
143+
verification_modal = <Modal> {
144+
content: {
145+
verification_modal_inner = <VerificationModal> {}
146+
}
145147
}
146148
}
147-
}
148-
} // end of body
149+
} // end of body
150+
}
149151
}
150152
}
151153
}
@@ -235,16 +237,13 @@ impl MatchEvent for App {
235237
cx.action(DockStateAction::RestoreFromAppState);
236238
}
237239
Some(RoomsPanelRestoreAction::RestoreWindow(window_state)) => {
238-
cx.push_unique_platform_op(CxOsOp::ResizeWindow(
239-
CxWindowPool::id_zero(),
240-
window_state.window_size.0,
241-
));
242-
cx.push_unique_platform_op(CxOsOp::RepositionWindow(
243-
CxWindowPool::id_zero(),
244-
window_state.window_position.0,
245-
));
246-
if window_state.window_is_fullscreen {
247-
cx.push_unique_platform_op(CxOsOp::MaximizeWindow(CxWindowPool::id_zero()));
240+
let window = self.ui.window(id!(main_window));
241+
window.resize(cx, window_state.inner_size.0);
242+
window.reposition(cx, window_state.position.0);
243+
if window_state.is_fullscreen {
244+
if let Some(mut window) = window.borrow_mut() {
245+
window.fullscreen(cx);
246+
}
248247
}
249248
}
250249
_ => {}
@@ -366,18 +365,21 @@ impl MatchEvent for App {
366365

367366
impl AppMain for App {
368367
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
369-
if let Event::WindowGeomChange(window_geom_change_event) = event {
370-
self.app_state.window_geom = Some(window_geom_change_event.new_geom.clone());
371-
}
372-
if let Event::WindowClosed(_) | Event::Shutdown = event {
373-
if let Some(window_geom) = &self.app_state.window_geom {
374-
let window_geom = window_geom.clone();
375-
cx.spawn_thread(move || {
376-
if let Err(e) = save_window_state(window_geom) {
377-
error!("Bug! Failed to save window_state: {}", e);
378-
}
379-
});
380-
}
368+
// Handling Event::Shutdown is not required as Window Closed event is guaranteed when closing.
369+
if let Event::WindowClosed(_) = event {
370+
let window = self.ui.window(id!(main_window));
371+
let inner_size = DVec2Wrapper(window.get_inner_size(cx));
372+
let position = DVec2Wrapper(window.get_position(cx));
373+
let window_geom = WindowGeomState{
374+
inner_size,
375+
position,
376+
is_fullscreen: window.is_fullscreen(cx),
377+
};
378+
cx.spawn_thread(move || {
379+
if let Err(e) = save_window_state(window_geom) {
380+
error!("Bug! Failed to save window_state: {}", e);
381+
}
382+
});
381383
if let Some(user_id) = current_user_id(){
382384
let rooms_panel = self.app_state.rooms_panel.clone();
383385
let user_id = user_id.clone();
@@ -446,8 +448,6 @@ pub struct AppState {
446448
pub rooms_panel: RoomsPanelState,
447449
/// Whether a user is currently logged in.
448450
pub logged_in: bool,
449-
/// The current window geometry.
450-
pub window_geom: Option<event::WindowGeom>,
451451
}
452452

453453
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
@@ -469,11 +469,11 @@ pub struct RoomsPanelState {
469469
/// The state of the window geometry
470470
pub struct WindowGeomState {
471471
/// A tuple containing the window's width and height.
472-
pub window_size: DVec2Wrapper,
472+
pub inner_size: DVec2Wrapper,
473473
/// A tuple containing the window's x and y position.
474-
pub window_position: DVec2Wrapper,
474+
pub position: DVec2Wrapper,
475475
/// Maximise fullscreen if true.
476-
pub window_is_fullscreen: bool
476+
pub is_fullscreen: bool
477477
}
478478

479479
/// Represents a room currently or previously selected by the user.

src/persistent_state.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::{collections::HashMap, path::PathBuf};
33
use anyhow::{anyhow, bail};
44
use makepad_widgets::{
5-
event::WindowGeom, log, makepad_micro_serde::{DeRon, SerRon}, Cx, DockItem, LiveId
5+
log, makepad_micro_serde::{DeRon, SerRon}, Cx, DockItem, LiveId
66
};
77
use matrix_sdk::{
88
authentication::matrix::MatrixSession,
@@ -213,19 +213,13 @@ pub fn save_room_panel(
213213
}
214214

215215
/// Save the current state of window geometry state to persistent storage.
216-
pub fn save_window_state(window_geom: WindowGeom) -> anyhow::Result<()> {
217-
let window_geom_state = WindowGeomState {
218-
window_is_fullscreen: window_geom.is_fullscreen,
219-
window_position: crate::utils::DVec2Wrapper(window_geom.position),
220-
window_size: crate::utils::DVec2Wrapper(window_geom.inner_size),
221-
};
216+
pub fn save_window_state(window_geom_state: WindowGeomState) -> anyhow::Result<()> {
222217
std::fs::write(
223218
app_data_dir().join(WINDOW_GEOM_STATE_FILE_NAME),
224219
serde_json::to_string(&window_geom_state)?,
225220
)?;
226221
Ok(())
227222
}
228-
229223
/// Loads the rooms panel's state from persistent storage.
230224
pub async fn load_rooms_panel_state(user_id: &UserId) -> anyhow::Result<RoomsPanelState> {
231225
let mut file = match tokio::fs::File::open(

0 commit comments

Comments
 (0)