Skip to content

Commit

Permalink
chore: some update
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Oct 6, 2024
1 parent 0d7d366 commit fb0e0f2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 51 deletions.
4 changes: 2 additions & 2 deletions iced_examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl Application for Counter {

fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::IcedEvent(event) => {
println!("hello {event:?}");
Message::IcedEvent(_event) => {
//println!("hello {event:?}");
Command::none()
}
Message::IncrementPressed => {
Expand Down
4 changes: 3 additions & 1 deletion iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ async fn run_instance<A, E, C>(
IcedLayerEvent::Window(event) => {
state.update(&event);

if let Some(event) = conversion::window_event(main_id, &event, state.modifiers()) {
if let Some(event) =
conversion::window_event(&event, state.scale_factor(), state.modifiers())
{
events.push(event);
}
}
Expand Down
52 changes: 20 additions & 32 deletions iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ where
A::Theme: DefaultStyle,
{
scale_factor: f64,
window_size: iced::Size<u32>,
window_scale_factor: u32,
window_scale_factor: f64,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -30,22 +29,13 @@ where
let theme = application.theme();
let appearance = application.style(&theme);

let window_scale_factor = 120;
let (window_size, viewport) = {
let (width, height) = window.main_window().get_size();

let realscale = window_scale_factor as f64 / 120.;
(
iced::Size::new(width, height),
Viewport::with_physical_size(
iced_core::Size::new(width, height),
realscale * scale_factor,
),
)
let window_scale_factor = 1.;
let (width, height) = window.main_window().get_size();
let viewport = {
Viewport::with_physical_size(iced_core::Size::new(width, height), window_scale_factor)
};
Self {
scale_factor,
window_size,
window_scale_factor,
viewport,
viewport_version: 0,
Expand All @@ -60,26 +50,22 @@ where
self.modifiers
}

pub fn scale_factor(&self) -> f64 {
self.viewport.scale_factor()
}

pub fn current_wayland_scale(&self) -> f64 {
self.window_scale_factor as f64 / 120.
self.window_scale_factor
}

pub fn update_view_port(&mut self, width: u32, height: u32) {
self.window_size = iced::Size::new(width, height);
self.viewport = Viewport::with_physical_size(
self.window_size(),
iced::Size::new(width, height),
self.current_wayland_scale() * self.scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
}

fn window_size(&self) -> iced::Size<u32> {
let mut window_size = self.window_size;
window_size.width = window_size.width * 140 / self.window_scale_factor;
window_size.height = window_size.height * 140 / self.window_scale_factor;
window_size
}

pub fn viewport(&self) -> &Viewport {
&self.viewport
}
Expand All @@ -106,6 +92,10 @@ 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)
}
Expand All @@ -123,15 +113,13 @@ where
}
WindowEvent::ScaleFactorChanged {
scale_float,
scale_u32,
scale_u32: _,
} => {
self.viewport = Viewport::with_physical_size(
self.window_size(),
self.scale_factor * scale_float,
);
let size = self.physical_size();
self.viewport = Viewport::with_physical_size(size, self.scale_factor * scale_float);

self.viewport_version = self.viewport_version.wrapping_add(1);
self.window_scale_factor = *scale_u32;
self.window_scale_factor = *scale_float;
}
_ => {}
}
Expand All @@ -141,7 +129,7 @@ where
let new_scale_factor = application.scale_factor();
if self.scale_factor != new_scale_factor {
self.viewport = Viewport::with_physical_size(
self.window_size(),
self.physical_size(),
self.current_wayland_scale() * new_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
Expand Down
40 changes: 29 additions & 11 deletions iced_layershell/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,33 @@ use layershellev::keyboard::KeyLocation;
use layershellev::keyboard::ModifiersState;
use layershellev::xkb_keyboard::ElementState;
use layershellev::xkb_keyboard::KeyEvent as LayerShellKeyEvent;
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 = x / scale_factor;
y = y / scale_factor;
(x.try_into().unwrap(), y.try_into().unwrap())
}

pub fn window_event(
#[allow(unused)] id: iced_core::window::Id,
layerevent: &LayerShellEvent,
scale_factor: f64,
modifiers: ModifiersState,
) -> Option<IcedEvent> {
match layerevent {
LayerShellEvent::CursorLeft => Some(IcedEvent::Mouse(mouse::Event::CursorLeft)),
LayerShellEvent::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 Down Expand Up @@ -83,38 +97,42 @@ pub fn window_event(
}
})),
LayerShellEvent::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,
},
}))
}
LayerShellEvent::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,
},
}))
}
LayerShellEvent::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,
},
}))
}
LayerShellEvent::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
7 changes: 5 additions & 2 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,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
17 changes: 14 additions & 3 deletions iced_layershell/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ where
{
id: window::Id,
scale_factor: f64,
wayland_scale_factor: f64,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -30,11 +31,15 @@ where
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 wayland_scale_factor = 1.0;
let viewport = Viewport::with_physical_size(
iced_core::Size::new(width, height),
wayland_scale_factor * scale_factor,
);
Self {
id,
scale_factor,
wayland_scale_factor,
viewport,
viewport_version: 0,
theme,
Expand All @@ -50,7 +55,8 @@ where
self.viewport = Viewport::with_physical_size(
iced_core::Size::new(width, height),
1. * self.scale_factor(),
)
);
self.viewport_version = self.viewport_version.wrapping_add(1);
}

pub fn viewport(&self) -> &Viewport {
Expand Down Expand Up @@ -87,6 +93,10 @@ 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)
}
Expand All @@ -106,6 +116,7 @@ where
scale_float,
scale_u32: _,
} => {
self.wayland_scale_factor = *scale_float;
let size = self.viewport.physical_size();

self.viewport = Viewport::with_physical_size(size, scale_float * self.scale_factor);
Expand Down

0 comments on commit fb0e0f2

Please sign in to comment.