Skip to content

Commit

Permalink
can create stateless custom widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Sep 24, 2024
1 parent d53907c commit ff2af9f
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 52 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

``` Rust
use iced::{Settings, Task, Theme};
use icy_browser::{get_fonts, Browser, Message};
use icy_browser::{get_fonts, BasicBrowser, Message};

fn run() -> (Browser, Task<Message>) {
fn run() -> (BasicBrowser, Task<Message>) {
(
Browser::new_with_ultralight()
BasicBrowser::new_basic()
.with_tab_bar()
.with_nav_bar()
.build(),
Expand All @@ -45,8 +45,8 @@ fn main() -> iced::Result {
..Default::default()
};

iced::application("Basic Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
iced::application("Basic Browser", BasicBrowser::update, BasicBrowser::view)
.subscription(BasicBrowser::subscription)
.settings(settings)
.theme(|_| Theme::Dark)
.run_with(run)
Expand Down
10 changes: 5 additions & 5 deletions examples/basic_browser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Simple browser with familiar browser widgets and the ultralight(webkit) webengine as a backend

use iced::{Settings, Task, Theme};
use icy_browser::{get_fonts, Browser, Message};
use icy_browser::{get_fonts, BasicBrowser, Message};

fn run() -> (Browser, Task<Message>) {
fn run() -> (BasicBrowser, Task<Message>) {
(
Browser::new_with_ultralight()
BasicBrowser::new_basic()
.with_tab_bar()
.with_nav_bar()
.build(),
Expand All @@ -19,8 +19,8 @@ fn main() -> iced::Result {
..Default::default()
};

iced::application("Basic Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
iced::application("Basic Browser", BasicBrowser::update, BasicBrowser::view)
.subscription(BasicBrowser::subscription)
.settings(settings)
.theme(|_| Theme::Dark)
.run_with(run)
Expand Down
121 changes: 121 additions & 0 deletions examples/custom_widgets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Custom view example with rainbow border

use iced::widget::{column, container};
use iced::{time, Border};
use iced::{Color, Element, Length, Settings, Subscription, Task, Theme};
use std::time::{Duration, Instant};

use icy_browser::{
browser_view, get_fonts, hoverable, nav_bar, tab_bar, widgets, BrowserEngine, BrowserWidget,
Ultralight,
};

fn main() -> iced::Result {
let settings = Settings {
fonts: get_fonts(),
..Default::default()
};

iced::application("Keyboard Driven Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
.settings(settings)
.theme(|_| Theme::Dark)
.run()
}

#[derive(Debug, Clone)]
pub enum Message {
BrowserWidget(widgets::Message), // Passes messagees to Browser widgets
Update,
Tick,
}

#[derive(Debug, Clone)]
struct CustomWidgetState {
border_colors: Vec<Color>,
start_time: Instant,
}

struct Browser {
widgets: BrowserWidget<Ultralight, CustomWidgetState>,
}

impl Default for Browser {
fn default() -> Self {
Self {
widgets: BrowserWidget::new()
.with_custom_view(
custom_view,
CustomWidgetState {
border_colors: vec![
Color::from_rgb(1.0, 0.0, 0.0), // Red
Color::from_rgb(1.0, 0.5, 0.0), // Orange
Color::from_rgb(1.0, 1.0, 0.0), // Yellow
Color::from_rgb(0.0, 1.0, 0.0), // Green
Color::from_rgb(0.0, 0.0, 1.0), // Blue
Color::from_rgb(0.29, 0.0, 0.51), // Indigo
Color::from_rgb(0.56, 0.0, 1.0), // Violet
],
start_time: Instant::now(),
},
)
.build(),
}
}
}

impl Browser {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::BrowserWidget(msg) => self.widgets.update(msg).map(Message::BrowserWidget),
Message::Update => self.widgets.force_update().map(Message::BrowserWidget),
Message::Tick => Task::none(), // Tick
}
}

fn view(&self) -> Element<Message> {
self.widgets.view().map(Message::BrowserWidget)
}

fn subscription(&self) -> Subscription<Message> {
Subscription::batch([
time::every(Duration::from_millis(10)).map(move |_| Message::Update),
time::every(Duration::from_millis(16)).map(|_| Message::Tick),
])
}
}

// fn custom_update<'a, Engine: BrowserEngine, CustomViewState>(
// browser_widget: &'a BrowserWidget<Engine, CustomWidgetState>,
// custom_view_state: &'a mut CustomViewState,
// ) -> Element<'a, icy_browser::Message> {

// }

fn custom_view<Engine: BrowserEngine>(
browser_widget: &BrowserWidget<Engine, CustomWidgetState>,
widget_state: CustomWidgetState,
) -> Element<icy_browser::Message> {
let elapsed = widget_state.start_time.elapsed().as_secs_f32();
let color_index = (elapsed * 2.0) as usize % widget_state.border_colors.len();
let color = widget_state.border_colors[color_index];

container(column![
tab_bar(browser_widget.engine().get_tabs()),
hoverable(nav_bar(&browser_widget.nav_bar_state))
.on_focus_change(icy_browser::Message::UpdateUrl),
browser_view(
browser_widget.view_size,
browser_widget.engine().get_tabs().get_current().get_view(),
!browser_widget.show_overlay,
),
])
.center_x(Length::Fill)
.center_y(Length::Fill)
.padding(20)
.style(move |_theme| container::Style {
border: Border::default().color(color).width(20),
..Default::default()
})
.into()
}
11 changes: 6 additions & 5 deletions examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use iced::{Element, Settings, Subscription, Task};
use std::time::Duration;

use icy_browser::{
get_fonts, widgets, BrowserWidget, KeyType, Message as WidgetMessage, ShortcutBuilder,
ShortcutModifier, Ultralight,
get_fonts, widgets, BasicBrowser, BrowserWidget, KeyType, Message as WidgetMessage,
ShortcutBuilder, ShortcutModifier,
};

fn main() -> iced::Result {
Expand All @@ -16,6 +16,8 @@ fn main() -> iced::Result {
..Default::default()
};

println!("Press Crtl - E to open to Command palatte");

iced::application("Keyboard Driven Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
.settings(settings)
Expand All @@ -31,7 +33,7 @@ pub enum Message {
}

struct Browser {
widgets: BrowserWidget<Ultralight>,
widgets: BasicBrowser,
}

impl Default for Browser {
Expand All @@ -45,10 +47,9 @@ impl Default for Browser {
],
)
.build();
let widgets = BrowserWidget::new_with_ultralight()
let widgets = BrowserWidget::new_basic()
.with_custom_shortcuts(shortcuts)
.with_tab_bar()
.with_nav_bar()
.build();

Self { widgets }
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use iced::widget::image::{Handle, Image};
pub use iced_fonts::BOOTSTRAP_FONT_BYTES;
pub use iced_on_focus_widget::hoverable;
use std::borrow::Cow;
use url::{ParseError, Url};

Expand All @@ -10,13 +11,17 @@ pub use engines::{BrowserEngine, PixelFormat, Tab, TabInfo, Tabs};
pub use engines::ultralight::Ultralight;

pub mod widgets;
pub use widgets::{nav_bar, tab_bar, BrowserWidget, Message};
pub use widgets::{browser_view, command_window, nav_bar, tab_bar, BrowserWidget, Message};

mod shortcut;
pub use shortcut::{KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts};

// Used when Not using custom views
#[derive(Clone)]
pub struct NoCustomView;

#[cfg(feature = "ultralight")]
pub type Browser = BrowserWidget<Ultralight>;
pub type BasicBrowser = BrowserWidget<Ultralight, NoCustomView>;

// Helper function to ensure required icons are imported
pub fn get_fonts() -> Vec<Cow<'static, [u8]>> {
Expand Down
Loading

0 comments on commit ff2af9f

Please sign in to comment.