Skip to content

Commit

Permalink
use Size form iced
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Aug 17, 2024
1 parent 7d4e3f3 commit 5d53e7d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/engines/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use iced::keyboard;
use iced::mouse::{self, Interaction};
use iced::Size;
// use iced::widget::image::{Handle, Image};
use iced::Point;
use url::Url;

use crate::ViewBounds;

#[cfg(feature = "webkit")]
pub mod ultralight;

Expand All @@ -29,7 +28,7 @@ pub trait BrowserEngine {
fn need_render(&self) -> bool;
fn render(&mut self);
fn size(&self) -> (u32, u32);
fn resize(&mut self, size: ViewBounds);
fn resize(&mut self, size: Size);
fn pixel_buffer(&mut self) -> (PixelFormat, Vec<u8>);

fn get_cursor(&self) -> Interaction;
Expand Down
7 changes: 3 additions & 4 deletions src/engines/ultralight.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::keyboard::{self};
use iced::mouse::{self, ScrollDelta};
use iced::Point;
use iced::{Point, Size};
use smol_str::SmolStr;
use std::sync::{Arc, RwLock};
use ul_next::{
Expand All @@ -16,7 +16,6 @@ use ul_next::{
use url::Url;

use super::{BrowserEngine, PixelFormat};
use crate::ViewBounds;

struct UlLogger;
impl Logger for UlLogger {
Expand Down Expand Up @@ -131,8 +130,8 @@ impl BrowserEngine for Ultralight {
(self.width, self.height)
}

fn resize(&mut self, size: ViewBounds) {
let (width, height) = (size.width, size.height);
fn resize(&mut self, size: Size) {
let (width, height) = (size.width as u32, size.height as u32);
(self.width, self.height) = (width, height);
self.tabs.iter().for_each(|tab| {
tab.view.resize(width, height);
Expand Down
21 changes: 0 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,6 @@ pub use engines::ultralight::Ultralight;
mod widgets;
pub use widgets::{browser_widgets, nav_bar, tab_bar, BrowserView, BrowserWidget};

#[derive(Debug, Clone, Copy)]
pub struct ViewBounds {
width: u32,
height: u32,
}

impl Default for ViewBounds {
fn default() -> Self {
Self {
width: 800,
height: 800,
}
}
}

impl ViewBounds {
pub fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
}

// Image details for passing the view around
#[derive(Debug, Clone)]
pub struct ImageInfo {
Expand Down
18 changes: 8 additions & 10 deletions src/widgets/browser_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ use iced::event::Status;
use iced::widget::image::{Handle, Image};
use iced::{theme::Theme, Element, Event, Length, Point, Rectangle, Size};

use crate::{ImageInfo, ViewBounds};
use crate::ImageInfo;

pub struct BrowserView<Message> {
bounds: ViewBounds,
bounds: Size,
image: Image<Handle>,
send_bounds: Box<dyn Fn(ViewBounds) -> Message>,
send_bounds: Box<dyn Fn(Size) -> Message>,
keyboard_event: Box<dyn Fn(iced::keyboard::Event) -> Message>,
mouse_event: Box<dyn Fn(Point, iced::mouse::Event) -> Message>,
}

impl<Message> BrowserView<Message> {
pub fn new(
image: &ImageInfo,
send_bounds: Box<dyn Fn(ViewBounds) -> Message>,
send_bounds: Box<dyn Fn(Size) -> Message>,
keyboard_event: Box<dyn Fn(iced::keyboard::Event) -> Message>,
mouse_event: Box<dyn Fn(Point, iced::mouse::Event) -> Message>,
) -> Self {
Self {
bounds: ViewBounds::default(),
bounds: Size::new(800., 800.),
image: image.as_image(),
send_bounds,
keyboard_event,
Expand Down Expand Up @@ -91,11 +91,9 @@ where
viewport: &Rectangle,
) -> event::Status {
// Send updates back if bounds change
if viewport.width as u32 != self.bounds.width
|| viewport.height as u32 != self.bounds.height
{
self.bounds.width = viewport.width as u32;
self.bounds.height = viewport.height as u32;
if viewport.width != self.bounds.width || viewport.height != self.bounds.height {
self.bounds.width = viewport.width;
self.bounds.height = viewport.height;
shell.publish((self.send_bounds)(self.bounds));
}

Expand Down
26 changes: 14 additions & 12 deletions src/widgets/browser_widgets.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use iced::{keyboard, mouse, widget::column, Element, Point};
use iced::{keyboard, mouse, widget::column, Element, Point, Size};
use url::Url;

use super::{nav_bar, tab_bar, BrowserView};
use crate::{
engines::{BrowserEngine, PixelFormat},
to_url, ImageInfo, ViewBounds,
to_url, ImageInfo,
};

#[cfg(feature = "ultralight")]
Expand All @@ -23,19 +23,19 @@ pub enum Message {
UrlChanged(String),
SendKeyboardEvent(keyboard::Event),
SendMouseEvent(Point, mouse::Event),
UpdateBounds(ViewBounds),
UpdateBounds(Size),
DoWork,
}

pub struct BrowserWidget<Engine: BrowserEngine> {
engine: Option<Engine>,
home: Url,
url: String,
tab_bar: bool,
nav_bar: bool,
url: String,
browser_view: bool,
image: ImageInfo,
view_bounds: ViewBounds,
view_bounds: Size,
}

impl<Engine: BrowserEngine> Default for BrowserWidget<Engine> {
Expand All @@ -44,12 +44,12 @@ impl<Engine: BrowserEngine> Default for BrowserWidget<Engine> {
Self {
engine: None,
home,
url: String::new(),
tab_bar: false,
nav_bar: false,
url: String::new(),
browser_view: false,
image: ImageInfo::default(),
view_bounds: ViewBounds::default(),
view_bounds: Size::new(800., 800.),
}
}
}
Expand Down Expand Up @@ -166,13 +166,15 @@ impl<Engine: BrowserEngine> BrowserWidget<Engine> {
if self.engine().need_render() {
let (format, image_data) = self.engine_mut().pixel_buffer();
self.image = match format {
PixelFormat::RGBA => {
ImageInfo::new(image_data, self.view_bounds.width, self.view_bounds.height)
}
PixelFormat::RGBA => ImageInfo::new(
image_data,
self.view_bounds.width as u32,
self.view_bounds.height as u32,
),
PixelFormat::BGRA => ImageInfo::new_from_bgr(
image_data,
self.view_bounds.width,
self.view_bounds.height,
self.view_bounds.width as u32,
self.view_bounds.height as u32,
),
}
}
Expand Down

0 comments on commit 5d53e7d

Please sign in to comment.