Skip to content

Commit

Permalink
Replace iced_web with WebGL support in wgpu 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jan 28, 2022
1 parent 1e3feee commit 825c774
Show file tree
Hide file tree
Showing 35 changed files with 69 additions and 3,475 deletions.
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["gui"]
resolver = "2"

[features]
default = ["wgpu", "default_system_font"]
default = ["wgpu"]
# Enables the `iced_wgpu` renderer
wgpu = ["iced_wgpu"]
# Enables the `Image` widget
Expand Down Expand Up @@ -58,7 +58,6 @@ members = [
"lazy",
"native",
"style",
"web",
"wgpu",
"winit",
"examples/bezier_tool",
Expand Down Expand Up @@ -94,16 +93,16 @@ members = [
[dependencies]
iced_core = { version = "0.4", path = "core" }
iced_futures = { version = "0.3", path = "futures" }
iced_winit = { version = "0.3", path = "winit" }
iced_glutin = { version = "0.2", path = "glutin", optional = true }
iced_glow = { version = "0.2", path = "glow", optional = true }
thiserror = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced_winit = { version = "0.3", path = "winit" }
iced_glutin = { version = "0.2", path = "glutin", optional = true }
iced_wgpu = { version = "0.4", path = "wgpu", optional = true }
iced_glow = { version = "0.2", path = "glow", optional = true}

[target.'cfg(target_arch = "wasm32")'.dependencies]
iced_web = { version = "0.4", path = "web" }
iced_wgpu = { version = "0.4", path = "wgpu", features = ["webgl"], optional = true }

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
78 changes: 17 additions & 61 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,28 @@ pub trait Application: Sized {
where
Self: 'static,
{
#[cfg(not(target_arch = "wasm32"))]
{
let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
text_multithreading: settings.text_multithreading,
antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else {
None
},
..crate::renderer::Settings::from_env()
};

Ok(crate::runtime::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::window::Compositor,
>(settings.into(), renderer_settings)?)
}

#[cfg(target_arch = "wasm32")]
{
<Instance<Self> as iced_web::Application>::run(settings.flags);

Ok(())
}
let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
text_multithreading: settings.text_multithreading,
antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else {
None
},
..crate::renderer::Settings::from_env()
};

Ok(crate::runtime::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::window::Compositor,
>(settings.into(), renderer_settings)?)
}
}

struct Instance<A: Application>(A);

#[cfg(not(target_arch = "wasm32"))]
impl<A> iced_winit::Program for Instance<A>
where
A: Application,
Expand All @@ -247,7 +236,6 @@ where
}
}

#[cfg(not(target_arch = "wasm32"))]
impl<A> crate::runtime::Application for Instance<A>
where
A: Application,
Expand Down Expand Up @@ -288,35 +276,3 @@ where
self.0.should_exit()
}
}

#[cfg(target_arch = "wasm32")]
impl<A> iced_web::Application for Instance<A>
where
A: Application,
{
type Executor = A::Executor;
type Message = A::Message;
type Flags = A::Flags;

fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
let (app, command) = A::new(flags);

(Instance(app), command)
}

fn title(&self) -> String {
self.0.title()
}

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
}

fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}

fn view(&mut self) -> Element<'_, Self::Message> {
self.0.view()
}
}
4 changes: 0 additions & 4 deletions src/element.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/// A generic widget.
///
/// This is an alias of an `iced_native` element with a default `Renderer`.
#[cfg(not(target_arch = "wasm32"))]
pub type Element<'a, Message> =
crate::runtime::Element<'a, Message, crate::renderer::Renderer>;

#[cfg(target_arch = "wasm32")]
pub use iced_web::Element;
1 change: 0 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub enum Error {
GraphicsAdapterNotFound,
}

#[cfg(not(target_arch = "wasm32"))]
impl From<iced_winit::Error> for Error {
fn from(error: iced_winit::Error) -> Error {
match error {
Expand Down
19 changes: 4 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,18 @@ pub mod window;
)]
pub mod time;

#[cfg(all(
not(target_arch = "wasm32"),
not(feature = "glow"),
feature = "wgpu"
))]
#[cfg(all(not(feature = "glow"), feature = "wgpu"))]
use iced_winit as runtime;

#[cfg(all(not(target_arch = "wasm32"), feature = "glow"))]
#[cfg(feature = "glow")]
use iced_glutin as runtime;

#[cfg(all(
not(target_arch = "wasm32"),
not(feature = "glow"),
feature = "wgpu"
))]
#[cfg(all(not(feature = "glow"), feature = "wgpu"))]
use iced_wgpu as renderer;

#[cfg(all(not(target_arch = "wasm32"), feature = "glow"))]
#[cfg(feature = "glow")]
use iced_glow as renderer;

#[cfg(target_arch = "wasm32")]
use iced_web as runtime;

#[doc(no_inline)]
pub use widget::*;

Expand Down
1 change: 0 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ where
}
}

#[cfg(not(target_arch = "wasm32"))]
impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
iced_winit::Settings {
Expand Down
96 changes: 43 additions & 53 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,53 @@
//!
//! These widgets have their own module with a `State` type. For instance, a
//! [`TextInput`] has some [`text_input::State`].
#[cfg(not(target_arch = "wasm32"))]
mod platform {
pub use crate::renderer::widget::{
button, checkbox, container, pane_grid, pick_list, progress_bar, radio,
rule, scrollable, slider, text_input, toggler, tooltip, Column, Row,
Space, Text,
};
pub use crate::renderer::widget::{
button, checkbox, container, pane_grid, pick_list, progress_bar, radio,
rule, scrollable, slider, text_input, toggler, tooltip, Column, Row, Space,
Text,
};

#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "canvas", feature = "glow_canvas")))
)]
pub use crate::renderer::widget::canvas;
#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "canvas", feature = "glow_canvas")))
)]
pub use crate::renderer::widget::canvas;

#[cfg(any(feature = "qr_code", feature = "glow_qr_code"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "qr_code", feature = "glow_qr_code")))
)]
pub use crate::renderer::widget::qr_code;
#[cfg(any(feature = "qr_code", feature = "glow_qr_code"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "qr_code", feature = "glow_qr_code")))
)]
pub use crate::renderer::widget::qr_code;

#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
pub mod image {
//! Display images in your user interface.
pub use crate::runtime::image::Handle;
pub use crate::runtime::widget::image::viewer;
pub use crate::runtime::widget::image::{Image, Viewer};
}

#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
pub mod svg {
//! Display vector graphics in your user interface.
pub use crate::runtime::svg::Handle;
pub use crate::runtime::widget::svg::Svg;
}

#[doc(no_inline)]
pub use {
button::Button, checkbox::Checkbox, container::Container, image::Image,
pane_grid::PaneGrid, pick_list::PickList, progress_bar::ProgressBar,
radio::Radio, rule::Rule, scrollable::Scrollable, slider::Slider,
svg::Svg, text_input::TextInput, toggler::Toggler, tooltip::Tooltip,
};

#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
#[doc(no_inline)]
pub use canvas::Canvas;

#[cfg(any(feature = "qr_code", feature = "glow_qr_code"))]
#[doc(no_inline)]
pub use qr_code::QRCode;
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
pub mod image {
//! Display images in your user interface.
pub use crate::runtime::image::Handle;
pub use crate::runtime::widget::image::viewer;
pub use crate::runtime::widget::image::{Image, Viewer};
}

#[cfg(target_arch = "wasm32")]
mod platform {
pub use iced_web::widget::*;
#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
pub mod svg {
//! Display vector graphics in your user interface.
pub use crate::runtime::svg::Handle;
pub use crate::runtime::widget::svg::Svg;
}

pub use platform::*;
#[doc(no_inline)]
pub use {
button::Button, checkbox::Checkbox, container::Container, image::Image,
pane_grid::PaneGrid, pick_list::PickList, progress_bar::ProgressBar,
radio::Radio, rule::Rule, scrollable::Scrollable, slider::Slider, svg::Svg,
text_input::TextInput, toggler::Toggler, tooltip::Tooltip,
};

#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
#[doc(no_inline)]
pub use canvas::Canvas;

#[cfg(any(feature = "qr_code", feature = "glow_qr_code"))]
#[doc(no_inline)]
pub use qr_code::QRCode;
19 changes: 0 additions & 19 deletions src/window/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ use std::fmt;
use std::io;

/// The icon of a window.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone)]
pub struct Icon(iced_winit::winit::window::Icon);

/// The icon of a window.
#[cfg(target_arch = "wasm32")]
#[derive(Debug, Clone)]
pub struct Icon;

impl Icon {
/// Creates an icon from 32bpp RGBA data.
#[cfg(not(target_arch = "wasm32"))]
pub fn from_rgba(
rgba: Vec<u8>,
width: u32,
Expand All @@ -25,16 +18,6 @@ impl Icon {

Ok(Icon(raw))
}

/// Creates an icon from 32bpp RGBA data.
#[cfg(target_arch = "wasm32")]
pub fn from_rgba(
_rgba: Vec<u8>,
_width: u32,
_height: u32,
) -> Result<Self, Error> {
Ok(Icon)
}
}

/// An error produced when using `Icon::from_rgba` with invalid arguments.
Expand Down Expand Up @@ -62,7 +45,6 @@ pub enum Error {
OsError(io::Error),
}

#[cfg(not(target_arch = "wasm32"))]
impl From<iced_winit::winit::window::BadIcon> for Error {
fn from(error: iced_winit::winit::window::BadIcon) -> Self {
use iced_winit::winit::window::BadIcon;
Expand All @@ -86,7 +68,6 @@ impl From<iced_winit::winit::window::BadIcon> for Error {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Icon> for iced_winit::winit::window::Icon {
fn from(icon: Icon) -> Self {
icon.0
Expand Down
1 change: 0 additions & 1 deletion src/window/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl Default for Position {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Position> for iced_winit::Position {
fn from(position: Position) -> Self {
match position {
Expand Down
1 change: 0 additions & 1 deletion src/window/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Default for Settings {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Settings> for iced_winit::settings::Window {
fn from(settings: Settings) -> Self {
Self {
Expand Down
Loading

3 comments on commit 825c774

@unreal79
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks Unicode support. See issue #33

@hecrj
Copy link
Member Author

@hecrj hecrj commented on 825c774 May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have never "supported Unicode".

@genusistimelord
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most Font handlers in Rust don't fully support Unicode glyph rendering needs yet. So it is hard to support Unicode.

Please sign in to comment.