Skip to content

Commit 4ce41e3

Browse files
committed
fix(uikit): layout on show
1 parent 38a2ba7 commit 4ce41e3

3 files changed

Lines changed: 66 additions & 21 deletions

File tree

mac_catalyst.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ APP_BUNDLE = ${BUILD_DIR}/${APP_NAME}.app
1616
APP_EXEC = ${APP_BUNDLE}/Contents/MacOS/${EXAMPLE}
1717
INFO_PLIST = winio-ui-ui-kit/Info.plist
1818

19-
.PHONY: all build bundle run debug clean
19+
.PHONY: all build bundle run open debug clean
2020

2121
all: bundle
2222

@@ -32,6 +32,9 @@ bundle: build ${INFO_PLIST}
3232
run: bundle
3333
${APP_EXEC}
3434

35+
open: bundle
36+
open ${APP_BUNDLE}
37+
3538
debug: bundle
3639
lldb -o run -- ${APP_EXEC}
3740

winio-ui-ui-kit/src/runtime.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use objc2_ui_kit::{
1717
UIWindowSceneDelegate, UIWindowSceneGeometry,
1818
};
1919
use slab::Slab;
20-
use winio_callback::Callback;
21-
use winio_primitive::Size;
20+
use winio_callback::{Callback, Runnable};
2221

23-
use crate::{Error, Result, from_cgsize};
22+
use crate::{Error, Result};
2423

2524
pub struct App {
2625
mtm: MainThreadMarker,
@@ -80,7 +79,24 @@ unsafe fn dispatcher_wake_by_ref(_: *const ()) {
8079
unsafe fn dispatcher_drop(_: *const ()) {}
8180

8281
thread_local! {
83-
pub(crate) static RESIZE_SLAB: RefCell<Slab<Rc<Callback<Size>>>> = const { RefCell::new(Slab::new()) };
82+
pub(crate) static RESIZE_SLAB: RefCell<Slab<Rc<Callback>>> = const { RefCell::new(Slab::new()) };
83+
pub(crate) static MOVE_SLAB: RefCell<Slab<Rc<Callback>>> = const { RefCell::new(Slab::new()) };
84+
}
85+
86+
fn signal_resize<R: Runnable>() {
87+
RESIZE_SLAB.with_borrow(|s| {
88+
for (_, callback) in s.iter() {
89+
callback.signal::<R>(());
90+
}
91+
});
92+
}
93+
94+
fn signal_move<R: Runnable>() {
95+
MOVE_SLAB.with_borrow(|s| {
96+
for (_, callback) in s.iter() {
97+
callback.signal::<R>(());
98+
}
99+
});
84100
}
85101

86102
#[derive(Debug)]
@@ -134,6 +150,12 @@ define_class! {
134150
) {
135151
winio_pollable::run_current_task();
136152
}
153+
154+
#[unsafe(method(sceneDidBecomeActive:))]
155+
fn sceneDidBecomeActive(&self, scene: &UIScene) {
156+
signal_resize::<()>();
157+
winio_pollable::run_current_task();
158+
}
137159
}
138160

139161
#[allow(non_snake_case)]
@@ -142,16 +164,17 @@ define_class! {
142164
fn windowScene_didUpdateEffectiveGeometry(
143165
&self,
144166
window_scene: &UIWindowScene,
145-
previous_effective_geometry: &UIWindowSceneGeometry,
167+
previous_effective_geometry: Option<&UIWindowSceneGeometry>,
146168
) {
147169
let geometry = window_scene.effectiveGeometry();
148170
let bounds = geometry.coordinateSpace(self.mtm()).bounds();
149-
let size = from_cgsize(bounds.size);
150-
RESIZE_SLAB.with_borrow(|s| {
151-
for (_, callback) in s.iter() {
152-
callback.signal::<()>(size);
153-
}
154-
});
171+
let previous_bounds = previous_effective_geometry.map(|g| g.coordinateSpace(self.mtm()).bounds());
172+
if Some(bounds.size) != previous_bounds.map(|b| b.size) {
173+
signal_resize::<()>();
174+
}
175+
if Some(bounds.origin) != previous_bounds.map(|b| b.origin) {
176+
signal_move::<()>();
177+
}
155178
winio_pollable::run_current_task();
156179
}
157180
}

winio-ui-ui-kit/src/ui/window.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@ use std::rc::Rc;
22

33
use inherit_methods_macro::inherit_methods;
44
use objc2::{MainThreadOnly, rc::Retained};
5-
use objc2_foundation::{MainThreadMarker, NSArray, NSSize};
5+
use objc2_foundation::{MainThreadMarker, NSArray, NSSize, NSString};
66
use objc2_ui_kit::{NSLayoutConstraint, UIView, UIViewController, UIWindow};
77
use winio_callback::Callback;
88
use winio_handle::{
99
AsContainer, AsWidget, AsWindow, BorrowedContainer, BorrowedWidget, BorrowedWindow,
1010
};
1111
use winio_primitive::{Point, Size};
12+
use winio_ui_apple_common::from_nsstring;
1213

1314
use crate::{
14-
Error, RESIZE_SLAB, Result, catch, first_ui_window_scene, from_cgpoint, from_cgsize,
15+
Error, MOVE_SLAB, RESIZE_SLAB, Result, catch, first_ui_window_scene, from_cgpoint, from_cgsize,
1516
to_cgpoint, to_cgsize,
1617
};
1718

1819
#[derive(Debug)]
1920
pub struct Window {
2021
wnd: Retained<UIWindow>,
2122
content_view: Retained<UIView>,
22-
did_resize: Rc<Callback<Size>>,
23+
did_resize: Rc<Callback>,
24+
did_move: Rc<Callback>,
2325
resize_index: usize,
26+
move_index: usize,
2427
}
2528

2629
impl Window {
@@ -35,7 +38,9 @@ impl Window {
3538
let controller = UIViewController::new(mtm);
3639

3740
let did_resize = Rc::new(Callback::new());
38-
let index = RESIZE_SLAB.with_borrow_mut(|s| s.insert(did_resize.clone()));
41+
let resize_index = RESIZE_SLAB.with_borrow_mut(|s| s.insert(did_resize.clone()));
42+
let did_move = Rc::new(Callback::new());
43+
let move_index = MOVE_SLAB.with_borrow_mut(|s| s.insert(did_move.clone()));
3944

4045
wnd.setRootViewController(Some(&controller));
4146
wnd.makeKeyWindow();
@@ -65,7 +70,9 @@ impl Window {
6570
wnd,
6671
content_view,
6772
did_resize,
68-
resize_index: index,
73+
resize_index,
74+
did_move,
75+
move_index,
6976
})
7077
})
7178
.flatten()
@@ -103,19 +110,30 @@ impl Window {
103110
}
104111

105112
pub fn text(&self) -> Result<String> {
106-
Ok(String::new())
113+
catch(|| {
114+
self.wnd
115+
.windowScene()
116+
.map(|s| s.title())
117+
.as_deref()
118+
.map(from_nsstring)
119+
.unwrap_or_default()
120+
})
107121
}
108122

109-
pub fn set_text(&mut self, _s: impl AsRef<str>) -> Result<()> {
110-
Ok(())
123+
pub fn set_text(&mut self, s: impl AsRef<str>) -> Result<()> {
124+
catch(|| {
125+
if let Some(scene) = self.wnd.windowScene() {
126+
scene.setTitle(Some(&NSString::from_str(s.as_ref())));
127+
}
128+
})
111129
}
112130

113131
pub async fn wait_size(&self) {
114132
self.did_resize.wait().await;
115133
}
116134

117135
pub async fn wait_move(&self) {
118-
std::future::pending().await
136+
self.did_move.wait().await;
119137
}
120138

121139
pub async fn wait_close(&self) {
@@ -130,6 +148,7 @@ impl Window {
130148
impl Drop for Window {
131149
fn drop(&mut self) {
132150
RESIZE_SLAB.with_borrow_mut(|s| s.remove(self.resize_index));
151+
MOVE_SLAB.with_borrow_mut(|s| s.remove(self.move_index));
133152
}
134153
}
135154

0 commit comments

Comments
 (0)