Skip to content

Commit

Permalink
feat: scale finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Oct 6, 2024
1 parent a2695d4 commit 606e53c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 44 deletions.
10 changes: 7 additions & 3 deletions iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ where

pub fn update(&mut self, event: &WindowEvent) {
match event {
WindowEvent::CursorLeft => {
WindowEvent::CursorLeft | WindowEvent::TouchUp { .. } => {
self.mouse_position = None;
}
WindowEvent::CursorMoved { x, y } => {
WindowEvent::CursorMoved { x, y }
| WindowEvent::CursorEnter { x, y }
| WindowEvent::TouchMotion { x, y, .. }
| WindowEvent::TouchDown { x, y, .. } => {
self.mouse_position = Some(Point::new(*x as f32, *y as f32));
}
WindowEvent::ModifiersChanged(modifiers) => {
Expand All @@ -117,7 +120,8 @@ where
scale_u32: _,
} => {
let size = self.physical_size();
self.viewport = Viewport::with_physical_size(size, self.application_scale_factor * scale_float);
self.viewport =
Viewport::with_physical_size(size, self.application_scale_factor * scale_float);

self.viewport_version = self.viewport_version.wrapping_add(1);
self.wayland_scale_factor = *scale_float;
Expand Down
7 changes: 5 additions & 2 deletions iced_layershell/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ where

pub fn update(&mut self, event: &WindowEvent) {
match event {
WindowEvent::CursorLeft => {
WindowEvent::CursorLeft | WindowEvent::TouchUp { .. } => {
self.mouse_position = None;
}
WindowEvent::CursorMoved { x, y } => {
WindowEvent::CursorMoved { x, y }
| WindowEvent::CursorEnter { x, y }
| WindowEvent::TouchMotion { x, y, .. }
| WindowEvent::TouchDown { x, y, .. } => {
self.mouse_position = Some(Point::new(*x as f32, *y as f32));
}
WindowEvent::ModifiersChanged(modifiers) => {
Expand Down
42 changes: 30 additions & 12 deletions iced_sessionlock/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@ use sessionlockev::xkb_keyboard::KeyEvent as SessionLockKeyEvent;
use iced_core::{keyboard, mouse, Event as IcedEvent};
use sessionlockev::keyboard::ModifiersState;

#[allow(unused)]
use std::ops::Mul;

fn scale_down<T>((x, y): (T, T), scale_factor: f64) -> (T, T)
where
T: Mul + TryInto<f64> + TryFrom<f64>,
<T as TryInto<f64>>::Error: std::fmt::Debug,
<T as TryFrom<f64>>::Error: std::fmt::Debug,
{
let (mut x, mut y): (f64, f64) = (x.try_into().unwrap(), y.try_into().unwrap());
x /= scale_factor;
y /= scale_factor;
(x.try_into().unwrap(), y.try_into().unwrap())
}

pub fn window_event(
id: iced_core::window::Id,
layerevent: &SessionLockEvent,
scale_factor: f64,

modifiers: ModifiersState,
) -> Option<IcedEvent> {
match layerevent {
SessionLockEvent::CursorLeft => Some(IcedEvent::Mouse(mouse::Event::CursorLeft)),
SessionLockEvent::CursorMoved { x, y } => {
let (x, y) = scale_down((*x, *y), scale_factor);
Some(IcedEvent::Mouse(mouse::Event::CursorMoved {
position: iced_core::Point {
x: *x as f32,
y: *y as f32,
x: x as f32,
y: y as f32,
},
}))
}
Expand All @@ -44,38 +58,42 @@ pub fn window_event(
}))
}
SessionLockEvent::TouchDown { id, x, y } => {
let (x, y) = scale_down((*x, *y), scale_factor);
Some(IcedEvent::Touch(touch::Event::FingerPressed {
id: touch::Finger(*id as u64),
position: iced::Point {
x: *x as f32,
y: *y as f32,
x: x as f32,
y: y as f32,
},
}))
}
SessionLockEvent::TouchUp { id, x, y } => {
let (x, y) = scale_down((*x, *y), scale_factor);
Some(IcedEvent::Touch(touch::Event::FingerLifted {
id: touch::Finger(*id as u64),
position: iced::Point {
x: *x as f32,
y: *y as f32,
x: x as f32,
y: y as f32,
},
}))
}
SessionLockEvent::TouchMotion { id, x, y } => {
let (x, y) = scale_down((*x, *y), scale_factor);
Some(IcedEvent::Touch(touch::Event::FingerMoved {
id: touch::Finger(*id as u64),
position: iced::Point {
x: *x as f32,
y: *y as f32,
x: x as f32,
y: y as f32,
},
}))
}
SessionLockEvent::TouchCancel { id, x, y } => {
let (x, y) = scale_down((*x, *y), scale_factor);
Some(IcedEvent::Touch(touch::Event::FingerLost {
id: touch::Finger(*id as u64),
position: iced::Point {
x: *x as f32,
y: *y as f32,
x: x as f32,
y: y as f32,
},
}))
}
Expand Down
1 change: 1 addition & 0 deletions iced_sessionlock/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub enum IcedSessionLockEvent<Message: 'static> {
RequestRefreshWithWrapper {
width: u32,
height: u32,
scale_float: f64,
wrapper: WindowWrapper,
},
#[allow(unused)]
Expand Down
26 changes: 15 additions & 11 deletions iced_sessionlock/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,18 @@ where
SessionLockEvent::BindProvide(_, _) => {}
SessionLockEvent::RequestMessages(message) => 'outside: {
match message {
DispatchMessage::RequestRefresh { width, height, .. } => {
DispatchMessage::RequestRefresh {
width,
height,
scale_float,
} => {
event_sender
.start_send(MultiWindowIcedSessionLockEvent(
id,
IcedSessionLockEvent::RequestRefreshWithWrapper {
width: *width,
height: *height,
scale_float: *scale_float,
wrapper: ev
.get_unit_with_id(id.unwrap())
.unwrap()
Expand Down Expand Up @@ -313,6 +318,7 @@ async fn run_instance<A, E, C>(
width,
height,
wrapper,
scale_float,
},
) => {
let (id, window) = if window_manager.get_mut_alias(wrapper.id()).is_none() {
Expand All @@ -321,6 +327,7 @@ async fn run_instance<A, E, C>(
let window = window_manager.insert(
id,
(width, height),
scale_float,
Arc::new(wrapper),
&application,
&mut compositor,
Expand Down Expand Up @@ -351,16 +358,10 @@ async fn run_instance<A, E, C>(
} else {
let (id, window) = window_manager.get_mut_alias(wrapper.id()).unwrap();
let ui = user_interfaces.remove(&id).expect("Get User interface");
window.state.update_view_port(width, height);
window.state.update_view_port(width, height, scale_float);
let _ = user_interfaces.insert(
id,
ui.relayout(
Size {
width: width as f32,
height: height as f32,
},
&mut window.renderer,
),
ui.relayout(window.state.logical_size(), &mut window.renderer),
);
(id, window)
};
Expand Down Expand Up @@ -430,8 +431,11 @@ async fn run_instance<A, E, C>(
continue;
};
window.state.update(&event);
if let Some(event) = conversion::window_event(id, &event, window.state.modifiers())
{
if let Some(event) = conversion::window_event(
&event,
window.state.scale_factor(),
window.state.modifiers(),
) {
events.push((Some(id), event));
}
}
Expand Down
62 changes: 47 additions & 15 deletions iced_sessionlock/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ where
A::Theme: DefaultStyle,
{
id: window::Id,
scale_factor: f64,
application_scale_factor: f64,
wayland_scale_factor: f64,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -25,16 +26,24 @@ impl<A: Application> State<A>
where
A::Theme: DefaultStyle,
{
pub fn new(id: window::Id, application: &A, (width, height): (u32, u32)) -> Self {
let scale_factor = application.scale_factor(id);
pub fn new(
id: window::Id,
application: &A,
(width, height): (u32, u32),
wayland_scale_factor: f64,
) -> Self {
let application_scale_factor = application.scale_factor(id);
let theme = application.theme();
let appearance = application.style(&theme);

let viewport =
Viewport::with_physical_size(iced_core::Size::new(width, height), 1. * scale_factor);
let viewport = Viewport::with_physical_size(
iced_core::Size::new(width, height),
wayland_scale_factor * application_scale_factor,
);
Self {
id,
scale_factor,
application_scale_factor,
wayland_scale_factor,
viewport,
viewport_version: 0,
theme,
Expand All @@ -46,11 +55,13 @@ where
pub fn modifiers(&self) -> ModifiersState {
self.modifiers
}
pub fn update_view_port(&mut self, width: u32, height: u32) {
pub fn update_view_port(&mut self, width: u32, height: u32, scale: f64) {
self.wayland_scale_factor = scale;
self.viewport = Viewport::with_physical_size(
iced_core::Size::new(width, height),
1. * self.scale_factor(),
)
self.wayland_scale_factor * self.application_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
}

pub fn viewport(&self) -> &Viewport {
Expand Down Expand Up @@ -83,32 +94,53 @@ where

pub fn cursor(&self) -> IcedMouse::Cursor {
self.mouse_position
.map(|point| Point {
x: point.x / self.scale_factor() as f32,
y: point.y / self.scale_factor() as f32,
})
.map(IcedMouse::Cursor::Available)
.unwrap_or(IcedMouse::Cursor::Unavailable)
}

pub fn update(&mut self, event: &WindowEvent) {
match event {
WindowEvent::CursorLeft => {
WindowEvent::CursorLeft | WindowEvent::TouchUp { .. } => {
self.mouse_position = None;
}
WindowEvent::CursorMoved { x, y } => {
WindowEvent::CursorMoved { x, y }
| WindowEvent::CursorEnter { x, y }
| WindowEvent::TouchMotion { x, y, .. }
| WindowEvent::TouchDown { x, y, .. } => {
self.mouse_position = Some(Point::new(*x as f32, *y as f32));
}
WindowEvent::ModifiersChanged(modifiers) => {
self.modifiers = *modifiers;
}
WindowEvent::ScaleFactorChanged {
scale_float,
scale_u32: _,
} => {
let size = self.viewport.physical_size();

self.viewport =
Viewport::with_physical_size(size, scale_float * self.application_scale_factor);

self.viewport_version = self.viewport_version.wrapping_add(1);
self.wayland_scale_factor = *scale_float;
}
_ => {}
}
}

pub fn synchronize(&mut self, application: &A) {
let new_scale_factor = application.scale_factor(self.id);
if self.scale_factor != new_scale_factor {
self.viewport =
Viewport::with_physical_size(self.physical_size(), 1. * new_scale_factor);
if self.application_scale_factor != new_scale_factor {
self.viewport = Viewport::with_physical_size(
self.physical_size(),
self.wayland_scale_factor * new_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
self.scale_factor = new_scale_factor;
self.application_scale_factor = new_scale_factor;
}
self.theme = application.theme();
self.appearance = application.style(&self.theme);
Expand Down
3 changes: 2 additions & 1 deletion iced_sessionlock/src/multi_window/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ where
&mut self,
id: IcedId,
size: (u32, u32),
fractal_scale: f64,
window: Arc<WindowWrapper>,
application: &A,
compositor: &mut C,
) -> &mut Window<A, C> {
let layerid = window.id();
let state = State::new(id, application, size);
let state = State::new(id, application, size, fractal_scale);
let physical_size = state.physical_size();
let surface = compositor.create_surface(window, physical_size.width, physical_size.height);
let renderer = compositor.create_renderer();
Expand Down

0 comments on commit 606e53c

Please sign in to comment.