Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign an Id to windows #131

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/widget-gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn app_view() -> impl View {
)
})
.style(|s| s.size(100.pct(), 100.pct()))
.window_title(|| "Widget Gallery".to_owned())
}

fn main() {
Expand Down
7 changes: 7 additions & 0 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#[derive(Clone, Default, Debug)]
pub struct IdPath(pub(crate) Vec<Id>);

impl IdPath {
/// Returns the slice of the ids excluding the first id identifying the window.
pub(crate) fn dispatch(&self) -> &[Id] {
&self.0[1..]
}

Check warning on line 39 in src/id.rs

View check run for this annotation

Codecov / codecov/patch

src/id.rs#L37-L39

Added lines #L37 - L39 were not covered by tests
}

impl Id {
/// Allocate a new, unique `Id`.
pub fn next() -> Id {
Expand Down
52 changes: 27 additions & 25 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
/// - requesting a new animation frame from the backend
pub(crate) struct WindowHandle {
pub(crate) window: Option<winit::window::Window>,
id: Id,
/// Reactive Scope for this WindowHandle
scope: Scope,
view: Box<dyn View>,
Expand All @@ -69,13 +70,20 @@
) -> Self {
let scope = Scope::new();
let window_id = window.id();
let id = Id::next();

Check warning on line 73 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L73

Added line #L73 was not covered by tests
let scale = window.scale_factor();
let size: LogicalSize<f64> = window.inner_size().to_logical(scale);
let size = Size::new(size.width, size.height);
let size = scope.create_rw_signal(Size::new(size.width, size.height));
let theme = scope.create_rw_signal(window.theme());
let is_maximized = window.is_maximized();

set_current_view(id);

ID_PATHS.with(|id_paths| {
id_paths.borrow_mut().insert(id, IdPath(vec![id]));
});

Check warning on line 86 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L81-L86

Added lines #L81 - L86 were not covered by tests
#[cfg(target_os = "linux")]
let context_menu = scope.create_rw_signal(None);

Expand All @@ -93,16 +101,13 @@
)
});

ID_PATHS.with(|id_paths| {
id_paths
.borrow_mut()
.insert(view.id(), IdPath(vec![view.id()]));
});
view.id().set_parent(id);

Check warning on line 104 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L104

Added line #L104 was not covered by tests
view_children_set_parent_id(&*view);

let paint_state = PaintState::new(&window, scale, size.get_untracked() * scale);
let mut window_handle = Self {
window: Some(window),
id,

Check warning on line 110 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L110

Added line #L110 was not covered by tests
scope,
view,
app_state: AppState::new(),
Expand All @@ -123,7 +128,7 @@
}

pub fn event(&mut self, event: Event) {
set_current_view(self.view.id());
set_current_view(self.id);

Check warning on line 131 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L131

Added line #L131 was not covered by tests
let event = event.scale(self.app_state.scale);

let mut cx = EventCx {
Expand Down Expand Up @@ -210,7 +215,7 @@
let id_path = ID_PATHS.with(|paths| paths.borrow().get(&id).cloned());
if let Some(id_path) = id_path {
self.view
.event_main(&mut cx, Some(&id_path.0), event.clone());
.event_main(&mut cx, Some(id_path.dispatch()), event.clone());

Check warning on line 218 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L218

Added line #L218 was not covered by tests
}
if let Event::PointerUp(_) = &event {
// To remove the styles applied by the Active selector
Expand All @@ -234,8 +239,8 @@
if view_state.hover_style.is_some()
|| view_state.active_style.is_some()
|| view_state.animation.is_some()
|| view_state.has_style_selectors.has(StyleSelector::Hover)
|| view_state.has_style_selectors.has(StyleSelector::Active)

Check warning on line 243 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L242-L243

Added lines #L242 - L243 were not covered by tests
{
cx.app_state.request_layout(*id);
}
Expand All @@ -246,8 +251,11 @@
} else {
let id_path = ID_PATHS.with(|paths| paths.borrow().get(id).cloned());
if let Some(id_path) = id_path {
self.view
.event_main(&mut cx, Some(&id_path.0), Event::PointerLeave);
self.view.event_main(
&mut cx,
Some(id_path.dispatch()),
Event::PointerLeave,
);

Check warning on line 258 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L254-L258

Added lines #L254 - L258 were not covered by tests
}
}
}
Expand Down Expand Up @@ -349,7 +357,7 @@
}

pub(crate) fn pointer_leave(&mut self) {
set_current_view(self.view.id());
set_current_view(self.id);

Check warning on line 360 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L360

Added line #L360 was not covered by tests
let mut cx = EventCx {
app_state: &mut self.app_state,
};
Expand All @@ -365,7 +373,7 @@
let id_path = ID_PATHS.with(|paths| paths.borrow().get(&id).cloned());
if let Some(id_path) = id_path {
self.view
.event_main(&mut cx, Some(&id_path.0), Event::PointerLeave);
.event_main(&mut cx, Some(id_path.dispatch()), Event::PointerLeave);

Check warning on line 376 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L376

Added line #L376 was not covered by tests
}
}
self.process_update();
Expand Down Expand Up @@ -553,11 +561,8 @@
let mut flags = ChangeFlags::empty();
loop {
self.process_central_messages();
let msgs = UPDATE_MESSAGES.with(|msgs| {
msgs.borrow_mut()
.remove(&self.view.id())
.unwrap_or_default()
});
let msgs =
UPDATE_MESSAGES.with(|msgs| msgs.borrow_mut().remove(&self.id).unwrap_or_default());

Check warning on line 565 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L564-L565

Added lines #L564 - L565 were not covered by tests
if msgs.is_empty() {
break;
}
Expand Down Expand Up @@ -609,7 +614,7 @@
UpdateMessage::State { id, state } => {
let id_path = ID_PATHS.with(|paths| paths.borrow().get(&id).cloned());
if let Some(id_path) = id_path {
flags |= self.view.update_main(&mut cx, &id_path.0, state);
flags |= self.view.update_main(&mut cx, id_path.dispatch(), state);

Check warning on line 617 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L617

Added line #L617 was not covered by tests
}
}
UpdateMessage::BaseStyle { id, style } => {
Expand Down Expand Up @@ -783,11 +788,8 @@
fn process_deferred_update_messages(&mut self) -> ChangeFlags {
self.process_central_messages();
let mut flags = ChangeFlags::empty();
let msgs = DEFERRED_UPDATE_MESSAGES.with(|msgs| {
msgs.borrow_mut()
.remove(&self.view.id())
.unwrap_or_default()
});
let msgs = DEFERRED_UPDATE_MESSAGES
.with(|msgs| msgs.borrow_mut().remove(&self.id).unwrap_or_default());

Check warning on line 792 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L791-L792

Added lines #L791 - L792 were not covered by tests
if msgs.is_empty() {
return flags;
}
Expand All @@ -798,7 +800,7 @@
for (id, state) in msgs {
let id_path = ID_PATHS.with(|paths| paths.borrow().get(&id).cloned());
if let Some(id_path) = id_path {
flags |= self.view.update_main(&mut cx, &id_path.0, state);
flags |= self.view.update_main(&mut cx, id_path.dispatch(), state);

Check warning on line 803 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L803

Added line #L803 was not covered by tests
}
}

Expand Down Expand Up @@ -889,7 +891,7 @@
fn has_deferred_update_messages(&self) -> bool {
DEFERRED_UPDATE_MESSAGES.with(|m| {
m.borrow()
.get(&self.view.id())
.get(&self.id)

Check warning on line 894 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L894

Added line #L894 was not covered by tests
.map(|m| !m.is_empty())
.unwrap_or(false)
})
Expand Down Expand Up @@ -1001,7 +1003,7 @@
}

pub(crate) fn menu_action(&mut self, id: usize) {
set_current_view(self.view.id());
set_current_view(self.id);

Check warning on line 1006 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L1006

Added line #L1006 was not covered by tests
if let Some(action) = self.app_state.window_menu.get(&id) {
(*action)();
self.process_update();
Expand Down
Loading