Skip to content

Commit

Permalink
clean up browser view with iced_event_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 11, 2024
1 parent 73a058a commit 88039cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 140 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 11 additions & 116 deletions src/widgets/browser_view.rs
Original file line number Diff line number Diff line change
@@ -1,121 +1,16 @@
use iced::advanced::{
self,
graphics::core::event,
layout, mouse,
renderer::{self},
widget::Tree,
Clipboard, Layout, Shell, Widget,
};
use iced::event::Status;
use iced::widget::image::{Handle, Image};
use iced::{theme::Theme, Element, Event, Length, Rectangle, Size};
use iced::{Element, Size};
use iced_event_wrapper::wrapper;

use super::Message;
use crate::ImageInfo;

pub fn browser_view(bounds: Size<u32>, image: &ImageInfo, can_type: bool) -> BrowserView {
BrowserView::new(bounds, image, can_type)
}

pub struct BrowserView {
bounds: Size<u32>,
image: Image<Handle>,
can_interact: bool, // wheather or not to allow typing - useful when overlay enabled
}

impl BrowserView {
pub fn new(bounds: Size<u32>, image: &ImageInfo, can_type: bool) -> Self {
Self {
bounds,
image: image.as_image(),
can_interact: can_type,
}
}
}

impl<Renderer> Widget<Message, Theme, Renderer> for BrowserView
where
Renderer: iced::advanced::image::Renderer<Handle = iced::advanced::image::Handle>,
{
fn size(&self) -> Size<Length> {
Size {
width: Length::Fill,
height: Length::Fill,
}
}

fn layout(
&self,
_tree: &mut Tree,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout::Node::new(limits.max())
}

fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
<Image<Handle> as Widget<Message, Theme, Renderer>>::draw(
&self.image,
tree,
renderer,
theme,
style,
layout,
cursor,
viewport,
)
}

fn on_event(
&mut self,
_state: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
if self.can_interact {
// Send updates back if bounds change
// convert to u32 because Image takes u32
let size = Size::new(layout.bounds().width as u32, layout.bounds().height as u32);
if self.bounds != size {
shell.publish(Message::UpdateViewSize(size));
}

match event {
Event::Keyboard(event) => {
shell.publish(Message::SendKeyboardEvent(Some(event)));
}
Event::Mouse(event) => {
if let Some(point) = cursor.position_in(layout.bounds()) {
shell.publish(Message::SendMouseEvent(point, Some(event)));
}
}
_ => (),
}
}
Status::Ignored
}
}

impl<'a, Message: 'a, Renderer> From<BrowserView> for Element<'a, Message, Theme, Renderer>
where
Renderer: advanced::Renderer + advanced::image::Renderer<Handle = advanced::image::Handle>,
BrowserView: Widget<Message, Theme, Renderer>,
{
fn from(widget: BrowserView) -> Self {
Self::new(widget)
}
pub fn browser_view(image: &ImageInfo) -> Element<Message> {
wrapper(image.as_image())
.always_ignore_events()
.on_keyboard_event(|event| Message::SendKeyboardEvent(Some(event)))
.on_mouse_event(|event, point| Message::SendMouseEvent(point, Some(event)))
.on_bounds_change(|bounds: Size| {
Message::UpdateViewSize(Size::new(bounds.width as u32, bounds.height as u32))
})
.into()
}
6 changes: 1 addition & 5 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
column = column.push(bookmark_bar(bookmarks))
}

let browser_view = browser_view(
self.view_size,
self.engine.get_tabs().get_current().get_view(),
!self.show_overlay,
);
let browser_view = browser_view(self.engine.get_tabs().get_current().get_view());
if self.show_overlay {
column = column.push(command_palatte(browser_view, &self.command_window_state))
} else {
Expand Down

0 comments on commit 88039cb

Please sign in to comment.