Skip to content

Commit

Permalink
feat: support fractal scale (#73)
Browse files Browse the repository at this point in the history
* feat: try to support scale

* chore: some update

* chore: clippy

* fix: set scale at the beginning

* chore: try set viewport

* fix: scale_problem

* feat: scale finished

* chore: go back to origin example
  • Loading branch information
Decodetalkers authored Oct 6, 2024
1 parent e1dbaf4 commit 1a61a63
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 123 deletions.
36 changes: 20 additions & 16 deletions iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,26 @@ async fn run_instance<A, E, C>(

while let Some(event) = event_receiver.next().await {
match event {
IcedLayerEvent::RequestRefresh { width, height } => {
state.update_view_port(width, height);
let ps = state.physical_size();
let width = ps.width;
let height = ps.height;
//state.update_view_port(width, height);
IcedLayerEvent::RequestRefresh {
width,
height,
fractal_scale,
} => {
state.update_view_port(width, height, fractal_scale);
let logical_size = state.logical_size();

debug.layout_started();
user_interface =
ManuallyDrop::new(ManuallyDrop::into_inner(user_interface).relayout(
Size {
width: width as f32,
height: height as f32,
},
&mut renderer,
));
user_interface = ManuallyDrop::new(
ManuallyDrop::into_inner(user_interface).relayout(logical_size, &mut renderer),
);
debug.layout_finished();

compositor.configure_surface(&mut surface, width, height);
let physical_size = state.physical_size();
compositor.configure_surface(
&mut surface,
physical_size.width,
physical_size.height,
);
let redraw_event =
IcedCoreEvent::Window(IcedCoreWindow::Event::RedrawRequested(Instant::now()));

Expand Down Expand Up @@ -468,7 +470,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
66 changes: 47 additions & 19 deletions iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub struct State<A: Application>
where
A::Theme: DefaultStyle,
{
scale_factor: f64,
application_scale_factor: f64,
wayland_scale_factor: f64,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -28,13 +29,14 @@ where
let theme = application.theme();
let appearance = application.style(&theme);

let wayland_scale_factor = 1.;
let (width, height) = window.main_window().get_size();
let viewport = {
let (width, height) = window.main_window().get_size();

Viewport::with_physical_size(iced_core::Size::new(width, height), 1. * scale_factor)
Viewport::with_physical_size(iced_core::Size::new(width, height), wayland_scale_factor)
};
Self {
scale_factor,
application_scale_factor: scale_factor,
wayland_scale_factor,
viewport,
viewport_version: 0,
theme,
Expand All @@ -48,11 +50,21 @@ where
self.modifiers
}

pub fn update_view_port(&mut self, width: u32, height: u32) {
pub fn scale_factor(&self) -> f64 {
self.viewport.scale_factor()
}

pub fn current_wayland_scale(&self) -> f64 {
self.wayland_scale_factor
}

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(),
)
iced::Size::new(width, height),
self.current_wayland_scale() * self.application_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
}

pub fn viewport(&self) -> &Viewport {
Expand All @@ -67,10 +79,6 @@ where
self.viewport.logical_size()
}

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

pub fn text_color(&self) -> Color {
self.appearance.text_color
}
Expand All @@ -85,32 +93,52 @@ 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.physical_size();
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;
}
_ => {}
}
}

pub fn synchronize(&mut self, application: &A) {
let new_scale_factor = application.scale_factor();
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.current_wayland_scale() * 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
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 /= scale_factor;
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
19 changes: 12 additions & 7 deletions iced_layershell/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ pub enum IcedLayerEvent<Message: 'static, INFO: Clone> {
RequestRefreshWithWrapper {
width: u32,
height: u32,
fractal_scale: f64,
wrapper: WindowWrapper,
is_created: bool,
info: Option<INFO>,
},
RequestRefresh {
width: u32,
height: u32,
fractal_scale: f64,
},
Window(WindowEvent),
NormalUpdate,
Expand All @@ -131,12 +133,16 @@ impl<Message: 'static, INFO: Clone> From<(Option<Id>, IcedLayerEvent<Message, IN
impl<Message: 'static, INFO: Clone> From<&DispatchMessage> for IcedLayerEvent<Message, INFO> {
fn from(value: &DispatchMessage) -> Self {
match value {
DispatchMessage::RequestRefresh { width, height, .. } => {
IcedLayerEvent::RequestRefresh {
width: *width,
height: *height,
}
}
DispatchMessage::RequestRefresh {
width,
height,
scale_float,
..
} => IcedLayerEvent::RequestRefresh {
width: *width,
height: *height,
fractal_scale: *scale_float,
},
DispatchMessage::MouseEnter {
surface_x: x,
surface_y: y,
Expand Down Expand Up @@ -188,7 +194,6 @@ impl<Message: 'static, INFO: Clone> From<&DispatchMessage> for IcedLayerEvent<Me
y: *y,
})
}

DispatchMessage::PreferredScale {
scale_u32,
scale_float,
Expand Down
21 changes: 11 additions & 10 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ where
DispatchMessage::RequestRefresh {
width,
height,
scale_float,
is_created,
..
} => {
Expand All @@ -268,6 +269,7 @@ where
IcedLayerEvent::RequestRefreshWithWrapper {
width: *width,
height: *height,
fractal_scale: *scale_float,
wrapper: unit.gen_wrapper(),
is_created: *is_created,
info: unit.get_binding().cloned(),
Expand Down Expand Up @@ -502,6 +504,7 @@ async fn run_instance<A, E, C>(
IcedLayerEvent::RequestRefreshWithWrapper {
width,
height,
fractal_scale,
wrapper,
is_created,
info,
Expand All @@ -515,6 +518,7 @@ async fn run_instance<A, E, C>(
let window = window_manager.insert(
id,
(width, height),
fractal_scale,
Arc::new(wrapper),
&application,
&mut compositor,
Expand Down Expand Up @@ -545,17 +549,11 @@ 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, fractal_scale);

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 @@ -656,8 +654,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
Loading

0 comments on commit 1a61a63

Please sign in to comment.