Skip to content

Commit

Permalink
ui: bump iced 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Jan 3, 2024
1 parent 051957e commit 1b315b5
Show file tree
Hide file tree
Showing 9 changed files with 1,330 additions and 289 deletions.
1,289 changes: 1,149 additions & 140 deletions gui/Cargo.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions gui/ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced = { version = "0.9", default_features = false, features = ["svg", "image", "glow"] }
iced_native = "0.10"
iced_lazy = { version = "0.6"}
iced = { version = "0.10", default_features = false, features = ["svg", "image", "lazy", "advanced"] }
bitcoin = "0.30"
chrono = "0.4"
3 changes: 1 addition & 2 deletions gui/ui/examples/design-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced = "0.9"
iced_native = "0.10"
iced = "0.10"
web-sys = "0.3.61"
chrono = "0.4"
liana_ui = { path = "../.." }
Expand Down
36 changes: 20 additions & 16 deletions gui/ui/examples/design-system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ mod section;

use iced::widget::{button, column, container, row, scrollable, text, Space};
use iced::{executor, Application, Command, Length, Settings, Subscription};
use liana_ui::{component::text::*, image, theme, widget::*};
use liana_ui::{component::text::*, font, image, theme, widget::*};

pub fn main() -> iced::Result {
let mut settings = Settings::with_flags(Config {});
settings.default_text_size = P1_SIZE.into();
settings.default_font = font::REGULAR;
DesignSystem::run(settings)
}

Expand All @@ -21,11 +22,18 @@ struct DesignSystem {

#[derive(Debug, Clone)]
pub enum Message {
Event(iced_native::Event),
FontLoaded(Result<(), iced::font::Error>),
Event(iced::Event),
Section(usize),
Ignore,
}

impl From<Result<(), iced::font::Error>> for Message {
fn from(res: Result<(), iced::font::Error>) -> Message {
Message::FontLoaded(res)
}
}

impl Application for DesignSystem {
type Message = Message;
type Theme = theme::Theme;
Expand All @@ -49,6 +57,9 @@ impl Application for DesignSystem {
],
current: 0,
};
#[allow(unused_mut)]
let mut cmds: Vec<Command<Self::Message>> = font::loads();

#[cfg(target_arch = "wasm32")]
{
use iced_native::{command, window};
Expand All @@ -57,16 +68,12 @@ impl Application for DesignSystem {
(window.inner_width().unwrap().as_f64().unwrap()) as u32,
(window.inner_height().unwrap().as_f64().unwrap()) as u32,
);
(
app,
Command::single(command::Action::Window(window::Action::Resize {
width,
height,
})),
)
cmds.push(Command::single(command::Action::Window(
window::Action::Resize { width, height },
)));
}
#[cfg(not(target_arch = "wasm32"))]
(app, Command::none())

(app, Command::batch(cmds))
}

fn update(&mut self, message: Message) -> Command<Self::Message> {
Expand All @@ -76,10 +83,7 @@ impl Application for DesignSystem {
self.current = i;
}
}
Message::Event(iced::Event::Window(iced_native::window::Event::Resized {
width,
height,
})) => {
Message::Event(iced::Event::Window(iced::window::Event::Resized { width, height })) => {
#[cfg(target_arch = "wasm32")]
{
use iced_native::{command, window};
Expand All @@ -95,7 +99,7 @@ impl Application for DesignSystem {
}

fn subscription(&self) -> Subscription<Self::Message> {
iced_native::subscription::events().map(Self::Message::Event)
iced::subscription::events().map(Self::Message::Event)
}

fn view(&self) -> Element<Message> {
Expand Down
5 changes: 2 additions & 3 deletions gui/ui/src/component/collapse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::widget::*;
use iced::widget::column;
use iced_lazy::{self, Component};
use iced::widget::{column, component, Component};
use std::marker::PhantomData;

pub struct Collapse<'a, M, H, F, C> {
Expand Down Expand Up @@ -78,6 +77,6 @@ where
C: Fn() -> Element<'a, T>,
{
fn from(c: Collapse<'a, Message, H, F, C>) -> Self {
iced_lazy::component(c)
component(c)
}
}
70 changes: 43 additions & 27 deletions gui/ui/src/component/modal.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/// modal widget from https://github.com/iced-rs/iced/blob/master/examples/modal/
use iced_native::alignment::Alignment;
use iced_native::widget::{self, Tree};
use iced_native::{
event, layout, mouse, overlay, renderer, Clipboard, Color, Element, Event, Layout, Length,
Point, Rectangle, Shell, Size, Widget,
};
use iced::advanced::layout::{self, Layout};
use iced::advanced::overlay;
use iced::advanced::renderer;
use iced::advanced::widget::{self, Tree, Widget};
use iced::advanced::{self, Clipboard, Shell};
use iced::alignment::Alignment;
use iced::event;
use iced::mouse;
use iced::{Color, Element, Event, Length, Point, Rectangle, Size};

/// A widget that centers a modal element over some base element
pub struct Modal<'a, Message, Renderer> {
Expand Down Expand Up @@ -35,7 +38,7 @@ impl<'a, Message, Renderer> Modal<'a, Message, Renderer> {

impl<'a, Message, Renderer> Widget<Message, Renderer> for Modal<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
Renderer: advanced::Renderer,
Message: Clone,
{
fn children(&self) -> Vec<Tree> {
Expand All @@ -60,33 +63,35 @@ where

fn on_event(
&mut self,
state: &mut Tree,
state: &mut widget::Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) -> event::Status {
self.base.as_widget_mut().on_event(
&mut state.children[0],
event,
layout,
cursor_position,
cursor,
renderer,
clipboard,
shell,
viewport,
)
}

fn draw(
&self,
state: &Tree,
state: &widget::Tree,
renderer: &mut Renderer,
theme: &<Renderer as iced_native::Renderer>::Theme,
theme: &<Renderer as advanced::Renderer>::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
self.base.as_widget().draw(
Expand All @@ -95,7 +100,7 @@ where
theme,
style,
layout,
cursor_position,
cursor,
viewport,
);
}
Expand All @@ -119,16 +124,16 @@ where

fn mouse_interaction(
&self,
state: &Tree,
state: &widget::Tree,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.base.as_widget().mouse_interaction(
&state.children[0],
layout,
cursor_position,
cursor,
viewport,
renderer,
)
Expand Down Expand Up @@ -157,7 +162,7 @@ struct Overlay<'a, 'b, Message, Renderer> {
impl<'a, 'b, Message, Renderer> overlay::Overlay<Message, Renderer>
for Overlay<'a, 'b, Message, Renderer>
where
Renderer: iced_native::Renderer,
Renderer: advanced::Renderer,
Message: Clone,
{
fn layout(&self, renderer: &Renderer, _bounds: Size, position: Point) -> layout::Node {
Expand All @@ -178,7 +183,7 @@ where
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
Expand All @@ -187,7 +192,7 @@ where

if let Some(message) = self.on_blur.as_ref() {
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) = &event {
if !content_bounds.contains(cursor_position) {
if !cursor.is_over(content_bounds) {
shell.publish(message.clone());
return event::Status::Captured;
}
Expand All @@ -198,10 +203,11 @@ where
self.tree,
event,
layout.children().next().unwrap(),
cursor_position,
cursor,
renderer,
clipboard,
shell,
&layout.bounds(),
)
}

Expand All @@ -211,12 +217,12 @@ where
theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
) {
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
border_radius: renderer::BorderRadius::from(0.0),
border_radius: Default::default(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Expand All @@ -232,7 +238,7 @@ where
theme,
style,
layout.children().next().unwrap(),
cursor_position,
cursor,
&layout.bounds(),
);
}
Expand All @@ -254,23 +260,33 @@ where
fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.content.as_widget().mouse_interaction(
self.tree,
layout.children().next().unwrap(),
cursor_position,
cursor,
viewport,
renderer,
)
}

fn overlay<'c>(
&'c mut self,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'c, Message, Renderer>> {
self.content
.as_widget_mut()
.overlay(self.tree, layout.children().next().unwrap(), renderer)
}
}

impl<'a, Message, Renderer> From<Modal<'a, Message, Renderer>> for Element<'a, Message, Renderer>
where
Renderer: 'a + iced_native::Renderer,
Renderer: 'a + advanced::Renderer,
Message: 'a + Clone,
{
fn from(modal: Modal<'a, Message, Renderer>) -> Self {
Expand Down
41 changes: 30 additions & 11 deletions gui/ui/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
use iced::Font;
use iced::{
font::{Family, Stretch, Weight},
Command, Font,
};

pub const BOLD: Font = Font::External {
name: "Bold",
bytes: include_bytes!("../static/fonts/IBMPlexSans-Bold.ttf"),
pub const BOLD: Font = Font {
family: Family::Name("IBM Plex Sans"),
weight: Weight::Bold,
monospaced: false,
stretch: Stretch::Normal,
};

pub const MEDIUM: Font = Font::External {
name: "Regular",
bytes: include_bytes!("../static/fonts/IBMPlexSans-Medium.ttf"),
pub const MEDIUM: Font = Font {
family: Family::Name("IBM Plex Sans"),
weight: Weight::Medium,
monospaced: false,
stretch: Stretch::Normal,
};

pub const REGULAR: Font = Font::with_name("IBM Plex Sans");

pub const BOLD_BYTES: &[u8] = include_bytes!("../static/fonts/IBMPlexSans-Bold.ttf");
pub const MEDIUM_BYTES: &[u8] = include_bytes!("../static/fonts/IBMPlexSans-Medium.ttf");
pub const REGULAR_BYTES: &[u8] = include_bytes!("../static/fonts/IBMPlexSans-Regular.ttf");

pub const REGULAR: Font = Font::External {
name: "Regular",
bytes: REGULAR_BYTES,
};
pub const ICONEX_ICONS_BYTES: &[u8] = include_bytes!("../static/icons/iconex/iconex-icons.ttf");
pub const BOOTSTRAP_ICONS_BYTE: &[u8] = include_bytes!("../static/icons/bootstrap-icons.ttf");

pub fn loads<T: From<Result<(), iced::font::Error>> + 'static>() -> Vec<Command<T>> {
vec![
iced::font::load(BOLD_BYTES).map(T::from),
iced::font::load(MEDIUM_BYTES).map(T::from),
iced::font::load(REGULAR_BYTES).map(T::from),
iced::font::load(ICONEX_ICONS_BYTES).map(T::from),
iced::font::load(BOOTSTRAP_ICONS_BYTE).map(T::from),
]
}
10 changes: 2 additions & 8 deletions gui/ui/src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::widget::*;
use iced::{alignment, Font, Length};

const BOOTSTRAP_ICONS: Font = Font::External {
name: "Bootstrap icons",
bytes: include_bytes!("../static/icons/bootstrap-icons.ttf"),
};
const BOOTSTRAP_ICONS: Font = Font::with_name("bootstrap-icons");

fn bootstrap_icon(unicode: char) -> Text<'static> {
Text::new(unicode.to_string())
Expand Down Expand Up @@ -118,10 +115,7 @@ pub fn previous_icon() -> Text<'static> {
bootstrap_icon('\u{F284}')
}

const ICONEX_ICONS: Font = Font::External {
name: "Iconex icons",
bytes: include_bytes!("../static/icons/iconex/iconex-icons.ttf"),
};
const ICONEX_ICONS: Font = Font::with_name("Untitled1");

fn iconex_icon(unicode: char) -> Text<'static> {
Text::new(unicode.to_string())
Expand Down
Loading

0 comments on commit 1b315b5

Please sign in to comment.