Skip to content

Commit

Permalink
chore: update iced
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Dec 10, 2024
1 parent 54b85f8 commit 282dfd1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 94 deletions.
23 changes: 19 additions & 4 deletions src/shell/element/stack/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct ScrollAnimationState {
start_time: Instant,
start: Offset,
end: Offset,
extra: Offset,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -74,13 +75,24 @@ impl Scrollable for State {
start_time: Instant::now(),
start: self.offset_x,
end: new_offset,
extra: Offset::Absolute(0.),
});
self.offset_x = new_offset;
}

/*fn scroll_by(&mut self, offset: AbsoluteOffset, bounds: Rectangle, content_bounds: Rectangle) {
todo!()
}*/
fn scroll_by(
&mut self,
offset: AbsoluteOffset,
_bounds: Rectangle,
_content_bounds: Rectangle,
) {
self.scroll_animation = Some(ScrollAnimationState {
start_time: Instant::now(),
start: self.offset_x,
end: self.offset_x,
extra: Offset::Absolute(offset.x.max(0.0)),
});
}
}

impl Default for State {
Expand Down Expand Up @@ -242,6 +254,7 @@ impl State {

Vector::new(
animation.start.absolute(bounds.width, content_bounds.width)
+ animation.extra.absolute(bounds.width, content_bounds.width) * percentage
+ (animation.end.absolute(bounds.width, content_bounds.width)
- animation.start.absolute(bounds.width, content_bounds.width))
* percentage,
Expand Down Expand Up @@ -673,7 +686,8 @@ where
) {
let state = tree.state.downcast_mut::<State>();
let bounds = layout.bounds();
// let content_bounds = todo!();
let content_layout = layout.children().next().unwrap();
let content_bounds = content_layout.bounds();

state.cleanup_old_animations();

Expand Down Expand Up @@ -824,6 +838,7 @@ where
start_time: Instant::now(),
start: Offset::Absolute(offset.x),
end: Offset::Absolute(new_offset.x),
extra: Offset::Absolute(0.),
});
state.offset_x = Offset::Absolute(new_offset.x);
}
Expand Down
160 changes: 70 additions & 90 deletions src/utils/iced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ use cosmic::{
iced::{
advanced::widget::Tree,
event::Event,
futures::{FutureExt, StreamExt},
keyboard::{Event as KeyboardEvent, Modifiers as IcedModifiers},
mouse::{Button as MouseButton, Cursor, Event as MouseEvent, ScrollDelta},
touch::{Event as TouchEvent, Finger},
window::Event as WindowEvent,
Limits, Point as IcedPoint, Rectangle as IcedRectangle, Size as IcedSize, Task,
Limits, Point as IcedPoint, Size as IcedSize, Task,
},
iced_core::{clipboard::Null as NullClipboard, id::Id, renderer::Style, Color, Length, Pixels},
iced_runtime::{
program::{Program as IcedProgram, State},
task::into_stream,
Action, Debug,
},
Theme,
};
use iced_tiny_skia::{
graphics::{damage, Viewport},
Primitive,
};
use iced_tiny_skia::{graphics::Viewport, Primitive};

use once_cell::sync::Lazy;
use ordered_float::OrderedFloat;
use smithay::{
backend::{
Expand Down Expand Up @@ -65,6 +65,8 @@ use smithay::{
},
};

static ID: Lazy<Id> = Lazy::new(|| Id::new("Program"));

pub struct IcedElement<P: Program + Send + 'static>(pub(crate) Arc<Mutex<IcedElementInternal<P>>>);

impl<P: Program + Send + 'static> fmt::Debug for IcedElement<P> {
Expand Down Expand Up @@ -157,9 +159,9 @@ pub(crate) struct IcedElementInternal<P: Program + Send + 'static> {

// futures
handle: LoopHandle<'static, crate::state::State>,
scheduler: Scheduler<<P as Program>::Message>,
scheduler: Scheduler<Option<<P as Program>::Message>>,
executor_token: Option<RegistrationToken>,
rx: Receiver<<P as Program>::Message>,
rx: Receiver<Option<<P as Program>::Message>>,
}

impl<P: Program + Send + Clone + 'static> Clone for IcedElementInternal<P> {
Expand All @@ -179,7 +181,7 @@ impl<P: Program + Send + Clone + 'static> Clone for IcedElementInternal<P> {
let mut renderer = cosmic::Renderer::new(cosmic::font::default(), Pixels(16.0));
let mut debug = Debug::new();
let state = State::new(
Id::unique(), // FIX: not sure if this works
ID.clone(), // FIX: not sure if this works
ProgramWrapper(self.state.program().0.clone(), handle.clone()),
IcedSize::new(self.size.w as f32, self.size.h as f32),
&mut renderer,
Expand Down Expand Up @@ -242,7 +244,7 @@ impl<P: Program + Send + 'static> IcedElement<P> {
let mut debug = Debug::new();

let state = State::new(
Id::unique(), // FIX: not sure if this works
ID.clone(),
ProgramWrapper(program, handle.clone()),
IcedSize::new(size.w as f32, size.h as f32),
&mut renderer,
Expand Down Expand Up @@ -359,7 +361,7 @@ impl<P: Program + Send + 'static + Clone> IcedElement<P> {
impl<P: Program + Send + 'static> IcedElementInternal<P> {
#[profiling::function]
fn update(&mut self, mut force: bool) -> Vec<Task<<P as Program>::Message>> {
while let Ok(message) = self.rx.try_recv() {
while let Ok(Some(message)) = self.rx.try_recv() {
self.state.queue_message(message);
force = true;
}
Expand All @@ -373,17 +375,16 @@ impl<P: Program + Send + 'static> IcedElementInternal<P> {
.map(|p| IcedPoint::new(p.x as f32, p.y as f32))
.map(Cursor::Available)
.unwrap_or(Cursor::Unavailable);

let actions = self
.state
.update(
Id::unique(), // FIX: not sure if this works
ID.clone(),
IcedSize::new(self.size.w as f32, self.size.h as f32),
cursor,
&mut self.renderer,
&self.theme,
&Style {
scale_factor: 1.0, //TODO: why is this
scale_factor: 1.0, // TODO: why is this
icon_color: self.theme.cosmic().on_bg_color().into(),
text_color: self.theme.cosmic().on_bg_color().into(),
},
Expand All @@ -392,17 +393,15 @@ impl<P: Program + Send + 'static> IcedElementInternal<P> {
)
.1;

actions
.into_iter()
.filter_map(|action| {
if let Action::Future(future) = action {
let _ = self.scheduler.schedule(future);
None
} else {
Some(action)
}
})
.collect::<Vec<_>>()
if let Some(action) = actions {
if let Some(t) = into_stream(action) {
let _ = self.scheduler.schedule(t.into_future().map(|f| match f.0 {
Some(Action::Output(msg)) => Some(msg),
_ => None,
}));
}
}
Vec::new()
}
}

Expand Down Expand Up @@ -847,83 +846,62 @@ where
} else {
false
};
if force {
internal_ref.pending_update = None;
}
let _ = internal_ref.update(force);

if let Some((buffer, ref mut old_primitives)) =
internal_ref.buffers.get_mut(&OrderedFloat(scale.x))
{
if let Some((buffer, _)) = internal_ref.buffers.get_mut(&OrderedFloat(scale.x)) {
let size: Size<i32, BufferCoords> = internal_ref
.size
.to_f64()
.to_buffer(scale.x, Transform::Normal)
.to_i32_round();

if size.w > 0 && size.h > 0 {
let cosmic::Renderer::TinySkia(renderer) = &mut internal_ref.renderer;
let state_ref = &internal_ref.state;
let mut clip_mask = tiny_skia::Mask::new(size.w as u32, size.h as u32).unwrap();
let overlay = internal_ref.debug.overlay();
let theme = &internal_ref.theme;

buffer
.render()
.draw(move |buf| {
let mut pixels =
tiny_skia::PixmapMut::from_bytes(buf, size.w as u32, size.h as u32)
.expect("Failed to create pixel map");

renderer.with_primitives(|backend, primitives| {
let background_color = state_ref.program().0.background_color(theme);
let bounds = IcedSize::new(size.w as u32, size.h as u32);
let viewport = Viewport::with_physical_size(bounds, scale.x);

let mut damage = old_primitives
.as_ref()
.and_then(|(last_primitives, last_color)| {
(last_color == &background_color)
.then(|| damage::list(last_primitives, primitives))
})
.unwrap_or_else(|| {
vec![IcedRectangle::with_size(viewport.logical_size())]
});
damage = damage::group(damage, scale.x as f32, bounds);

if !damage.is_empty() {
backend.draw(
&mut pixels,
&mut clip_mask,
primitives,
&viewport,
&damage,
background_color,
&overlay,
);

*old_primitives = Some((primitives.to_vec(), background_color));
}

let damage = damage
.into_iter()
.map(|x| x.snap())
.map(|damage_rect| {
Rectangle::from_loc_and_size(
(damage_rect.x as i32, damage_rect.y as i32),
(damage_rect.width as i32, damage_rect.height as i32),
)
})
.collect::<Vec<_>>();

state_ref.program().0.foreground(
&mut pixels,
&damage,
scale.x as f32,
theme,
);

Result::<_, ()>::Ok(damage)
_ = buffer.render().draw(|buf| {
let mut pixels =
tiny_skia::PixmapMut::from_bytes(buf, size.w as u32, size.h as u32)
.expect("Failed to create pixel map");

let background_color = state_ref.program().0.background_color(theme);
let bounds = IcedSize::new(size.w as u32, size.h as u32);
let viewport = Viewport::with_physical_size(bounds, scale.x);

let damage = vec![cosmic::iced::Rectangle::new(
cosmic::iced::Point::default(),
viewport.logical_size(),
)];

internal_ref.renderer.draw(
&mut pixels,
&mut clip_mask,
&viewport,
&damage,
background_color,
&overlay,
);

let damage = damage
.into_iter()
.filter_map(|x| x.snap())
.map(|damage_rect| {
Rectangle::from_loc_and_size(
(damage_rect.x as i32, damage_rect.y as i32),
(damage_rect.width as i32, damage_rect.height as i32),
)
})
})
.unwrap();
.collect::<Vec<_>>();
state_ref
.program()
.0
.foreground(&mut pixels, &damage, scale.x as f32, theme);

Result::<_, ()>::Ok(damage)
});
}

if let Ok(buffer) = MemoryRenderBufferRenderElement::from_buffer(
Expand All @@ -933,9 +911,11 @@ where
Some(alpha),
Some(Rectangle::from_loc_and_size(
(0., 0.),
size.to_f64().to_logical(1.0, Transform::Normal),
size.to_f64()
.to_logical(1., Transform::Normal)
.to_i32_round(),
)),
Some(internal_ref.size),
Some(size.to_logical(1, Transform::Normal)),
Kind::Unspecified,
) {
return vec![C::from(buffer)];
Expand Down

0 comments on commit 282dfd1

Please sign in to comment.