diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/multi_window.rs | 4 | ||||
| -rw-r--r-- | src/multi_window/application.rs | 254 | ||||
| -rw-r--r-- | src/window.rs | 11 | ||||
| -rw-r--r-- | src/window/icon.rs | 169 | ||||
| -rw-r--r-- | src/window/position.rs | 32 | ||||
| -rw-r--r-- | src/window/settings.rs | 70 | 
7 files changed, 264 insertions, 279 deletions
| @@ -182,6 +182,9 @@ pub mod touch;  pub mod widget;  pub mod window; +#[cfg(feature = "multi_window")] +pub mod multi_window; +  #[cfg(all(not(feature = "glow"), feature = "wgpu"))]  use iced_winit as runtime; diff --git a/src/multi_window.rs b/src/multi_window.rs new file mode 100644 index 00000000..5b7a00b4 --- /dev/null +++ b/src/multi_window.rs @@ -0,0 +1,4 @@ +//! Leverage multi-window support in your application. +mod application; + +pub use application::Application; diff --git a/src/multi_window/application.rs b/src/multi_window/application.rs new file mode 100644 index 00000000..d0b515ab --- /dev/null +++ b/src/multi_window/application.rs @@ -0,0 +1,254 @@ +use crate::window; +use crate::{Command, Element, Executor, Settings, Subscription}; + +pub use iced_native::application::{Appearance, StyleSheet}; + +/// An interactive cross-platform multi-window application. +/// +/// This trait is the main entrypoint of Iced. Once implemented, you can run +/// your GUI application by simply calling [`run`](#method.run). +/// +/// An [`Application`] can execute asynchronous actions by returning a +/// [`Command`] in some of its methods. For example, to spawn a new window, you +/// can use the `iced_winit::window::spawn()` [`Command`]. +/// +/// When using an [`Application`] with the `debug` feature enabled, a debug view +/// can be toggled by pressing `F12`. +/// +/// ## A simple "Hello, world!" +/// +/// If you just want to get started, here is a simple [`Application`] that +/// says "Hello, world!": +/// +/// ```no_run +/// use iced::executor; +/// use iced::multi_window::Application; +/// use iced::window; +/// use iced::{Command, Element, Settings, Theme}; +/// +/// pub fn main() -> iced::Result { +///     Hello::run(Settings::default()) +/// } +/// +/// struct Hello; +/// +/// impl Application for Hello { +///     type Executor = executor::Default; +///     type Message = (); +///     type Theme = Theme; +///     type Flags = (); +/// +///     fn new(_flags: ()) -> (Hello, Command<Self::Message>) { +///         (Hello, Command::none()) +///     } +/// +///     fn title(&self, window: window::Id) -> String { +///         String::from("A cool application") +///     } +/// +///     fn update(&mut self, _message: Self::Message) -> Command<Self::Message> { +///         Command::none() +///     } +/// +///     fn view(&self, window: window::Id) -> Element<Self::Message> { +///         "Hello, world!".into() +///     } +/// +///     fn close_requested(&self, window: window::Id) -> Self::Message { +///         () +///     } +/// } +/// ``` +pub trait Application: Sized { +    /// The [`Executor`] that will run commands and subscriptions. +    /// +    /// The [default executor] can be a good starting point! +    /// +    /// [`Executor`]: Self::Executor +    /// [default executor]: crate::executor::Default +    type Executor: Executor; + +    /// The type of __messages__ your [`Application`] will produce. +    type Message: std::fmt::Debug + Send; + +    /// The theme of your [`Application`]. +    type Theme: Default + StyleSheet; + +    /// The data needed to initialize your [`Application`]. +    type Flags; + +    /// Initializes the [`Application`] with the flags provided to +    /// [`run`] as part of the [`Settings`]. +    /// +    /// Here is where you should return the initial state of your app. +    /// +    /// Additionally, you can return a [`Command`] if you need to perform some +    /// async action in the background on startup. This is useful if you want to +    /// load state from a file, perform an initial HTTP request, etc. +    /// +    /// [`run`]: Self::run +    fn new(flags: Self::Flags) -> (Self, Command<Self::Message>); + +    /// Returns the current title of the [`Application`]. +    /// +    /// This title can be dynamic! The runtime will automatically update the +    /// title of your application when necessary. +    fn title(&self, window: window::Id) -> String; + +    /// Handles a __message__ and updates the state of the [`Application`]. +    /// +    /// This is where you define your __update logic__. All the __messages__, +    /// produced by either user interactions or commands, will be handled by +    /// this method. +    /// +    /// Any [`Command`] returned will be executed immediately in the background. +    fn update(&mut self, message: Self::Message) -> Command<Self::Message>; + +    /// Returns the current [`Theme`] of the [`Application`]. +    /// +    /// [`Theme`]: Self::Theme +    fn theme(&self) -> Self::Theme { +        Self::Theme::default() +    } + +    /// Returns the current [`Style`] of the [`Theme`]. +    /// +    /// [`Style`]: <Self::Theme as StyleSheet>::Style +    /// [`Theme`]: Self::Theme +    fn style(&self) -> <Self::Theme as StyleSheet>::Style { +        <Self::Theme as StyleSheet>::Style::default() +    } + +    /// Returns the event [`Subscription`] for the current state of the +    /// application. +    /// +    /// A [`Subscription`] will be kept alive as long as you keep returning it, +    /// and the __messages__ produced will be handled by +    /// [`update`](#tymethod.update). +    /// +    /// By default, this method returns an empty [`Subscription`]. +    fn subscription(&self) -> Subscription<Self::Message> { +        Subscription::none() +    } + +    /// Returns the widgets to display in the [`Application`]. +    /// +    /// These widgets can produce __messages__ based on user interaction. +    fn view( +        &self, +        window: window::Id, +    ) -> Element<'_, Self::Message, crate::Renderer<Self::Theme>>; + +    /// Returns the scale factor of the [`Application`]. +    /// +    /// It can be used to dynamically control the size of the UI at runtime +    /// (i.e. zooming). +    /// +    /// For instance, a scale factor of `2.0` will make widgets twice as big, +    /// while a scale factor of `0.5` will shrink them to half their size. +    /// +    /// By default, it returns `1.0`. +    #[allow(unused_variables)] +    fn scale_factor(&self, window: window::Id) -> f64 { +        1.0 +    } + +    /// Returns whether the [`Application`] should be terminated. +    /// +    /// By default, it returns `false`. +    fn should_exit(&self) -> bool { +        false +    } + +    /// Requests that the [`window`] be closed. +    fn close_requested(&self, window: window::Id) -> Self::Message; + +    /// Runs the [`Application`]. +    /// +    /// On native platforms, this method will take control of the current thread +    /// until the [`Application`] exits. +    /// +    /// On the web platform, this method __will NOT return__ unless there is an +    /// [`Error`] during startup. +    /// +    /// [`Error`]: crate::Error +    fn run(settings: Settings<Self::Flags>) -> crate::Result +    where +        Self: 'static, +    { +        #[allow(clippy::needless_update)] +        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::multi_window::run::< +            Instance<Self>, +            Self::Executor, +            crate::renderer::window::Compositor<Self::Theme>, +        >(settings.into(), renderer_settings)?) +    } +} + +struct Instance<A: Application>(A); + +impl<A> crate::runtime::multi_window::Application for Instance<A> +where +    A: Application, +{ +    type Flags = A::Flags; +    type Renderer = crate::Renderer<A::Theme>; +    type Message = A::Message; + +    fn update(&mut self, message: Self::Message) -> Command<Self::Message> { +        self.0.update(message) +    } + +    fn view( +        &self, +        window: window::Id, +    ) -> Element<'_, Self::Message, Self::Renderer> { +        self.0.view(window) +    } + +    fn new(flags: Self::Flags) -> (Self, Command<A::Message>) { +        let (app, command) = A::new(flags); + +        (Instance(app), command) +    } + +    fn title(&self, window: window::Id) -> String { +        self.0.title(window) +    } + +    fn theme(&self) -> A::Theme { +        self.0.theme() +    } + +    fn style(&self) -> <A::Theme as StyleSheet>::Style { +        self.0.style() +    } + +    fn subscription(&self) -> Subscription<Self::Message> { +        self.0.subscription() +    } + +    fn scale_factor(&self, window: window::Id) -> f64 { +        self.0.scale_factor(window) +    } + +    fn should_exit(&self) -> bool { +        self.0.should_exit() +    } + +    fn close_requested(&self, window: window::Id) -> Self::Message { +        self.0.close_requested(window) +    } +} diff --git a/src/window.rs b/src/window.rs index 2018053f..73e90243 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,12 +1,7 @@  //! Configure the window of your application in native platforms. -mod position; -mod settings; - -pub mod icon; - -pub use icon::Icon; -pub use position::Position; -pub use settings::Settings; +pub use iced_native::window::Icon; +pub use iced_native::window::Position; +pub use iced_native::window::Settings;  #[cfg(not(target_arch = "wasm32"))]  pub use crate::runtime::window::*; diff --git a/src/window/icon.rs b/src/window/icon.rs deleted file mode 100644 index d57eb79c..00000000 --- a/src/window/icon.rs +++ /dev/null @@ -1,169 +0,0 @@ -//! Attach an icon to the window of your application. -use std::fmt; -use std::io; - -#[cfg(feature = "image_rs")] -use std::path::Path; - -/// The icon of a window. -#[derive(Debug, Clone)] -pub struct Icon(iced_winit::winit::window::Icon); - -impl Icon { -    /// Creates an icon from 32bpp RGBA data. -    pub fn from_rgba( -        rgba: Vec<u8>, -        width: u32, -        height: u32, -    ) -> Result<Self, Error> { -        let raw = -            iced_winit::winit::window::Icon::from_rgba(rgba, width, height)?; - -        Ok(Icon(raw)) -    } - -    /// Creates an icon from an image file. -    /// -    /// This will return an error in case the file is missing at run-time. You may prefer [`Self::from_file_data`] instead. -    #[cfg(feature = "image_rs")] -    pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Self, Error> { -        let icon = image_rs::io::Reader::open(icon_path)?.decode()?.to_rgba8(); - -        Self::from_rgba(icon.to_vec(), icon.width(), icon.height()) -    } - -    /// Creates an icon from the content of an image file. -    /// -    /// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro. \ -    /// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime. -    #[cfg(feature = "image_rs")] -    pub fn from_file_data( -        data: &[u8], -        explicit_format: Option<image_rs::ImageFormat>, -    ) -> Result<Self, Error> { -        let mut icon = image_rs::io::Reader::new(std::io::Cursor::new(data)); -        let icon_with_format = match explicit_format { -            Some(format) => { -                icon.set_format(format); -                icon -            } -            None => icon.with_guessed_format()?, -        }; - -        let pixels = icon_with_format.decode()?.to_rgba8(); - -        Self::from_rgba(pixels.to_vec(), pixels.width(), pixels.height()) -    } -} - -/// An error produced when using `Icon::from_rgba` with invalid arguments. -#[derive(Debug)] -pub enum Error { -    /// The provided RGBA data isn't divisble by 4. -    /// -    /// Therefore, it cannot be safely interpreted as 32bpp RGBA pixels. -    InvalidData { -        /// The length of the provided RGBA data. -        byte_count: usize, -    }, - -    /// The number of RGBA pixels does not match the provided dimensions. -    DimensionsMismatch { -        /// The provided width. -        width: u32, -        /// The provided height. -        height: u32, -        /// The amount of pixels of the provided RGBA data. -        pixel_count: usize, -    }, - -    /// The underlying OS failed to create the icon. -    OsError(io::Error), - -    /// The `image` crate reported an error -    #[cfg(feature = "image_rs")] -    ImageError(image_rs::error::ImageError), -} - -impl From<std::io::Error> for Error { -    fn from(os_error: std::io::Error) -> Self { -        Error::OsError(os_error) -    } -} - -impl From<iced_winit::winit::window::BadIcon> for Error { -    fn from(error: iced_winit::winit::window::BadIcon) -> Self { -        use iced_winit::winit::window::BadIcon; - -        match error { -            BadIcon::ByteCountNotDivisibleBy4 { byte_count } => { -                Error::InvalidData { byte_count } -            } -            BadIcon::DimensionsVsPixelCount { -                width, -                height, -                pixel_count, -                .. -            } => Error::DimensionsMismatch { -                width, -                height, -                pixel_count, -            }, -            BadIcon::OsError(os_error) => Error::OsError(os_error), -        } -    } -} - -impl From<Icon> for iced_winit::winit::window::Icon { -    fn from(icon: Icon) -> Self { -        icon.0 -    } -} - -#[cfg(feature = "image_rs")] -impl From<image_rs::error::ImageError> for Error { -    fn from(image_error: image_rs::error::ImageError) -> Self { -        Self::ImageError(image_error) -    } -} - -impl fmt::Display for Error { -    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -        match self { -            Error::InvalidData { byte_count } => { -                write!( -                    f, -                    "The provided RGBA data (with length {byte_count:?}) isn't divisble by \ -                4. Therefore, it cannot be safely interpreted as 32bpp RGBA \ -                pixels." -                ) -            } -            Error::DimensionsMismatch { -                width, -                height, -                pixel_count, -            } => { -                write!( -                    f, -                    "The number of RGBA pixels ({pixel_count:?}) does not match the provided \ -                dimensions ({width:?}x{height:?})." -                ) -            } -            Error::OsError(e) => write!( -                f, -                "The underlying OS failed to create the window \ -                icon: {e:?}" -            ), -            #[cfg(feature = "image_rs")] -            Error::ImageError(e) => { -                write!(f, "Unable to create icon from a file: {e:?}") -            } -        } -    } -} - -impl std::error::Error for Error { -    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { -        Some(self) -    } -} diff --git a/src/window/position.rs b/src/window/position.rs deleted file mode 100644 index 6b9fac41..00000000 --- a/src/window/position.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// The position of a window in a given screen. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Position { -    /// The platform-specific default position for a new window. -    Default, -    /// The window is completely centered on the screen. -    Centered, -    /// The window is positioned with specific coordinates: `(X, Y)`. -    /// -    /// When the decorations of the window are enabled, Windows 10 will add some -    /// invisible padding to the window. This padding gets included in the -    /// position. So if you have decorations enabled and want the window to be -    /// at (0, 0) you would have to set the position to -    /// `(PADDING_X, PADDING_Y)`. -    Specific(i32, i32), -} - -impl Default for Position { -    fn default() -> Self { -        Self::Default -    } -} - -impl From<Position> for iced_winit::Position { -    fn from(position: Position) -> Self { -        match position { -            Position::Default => Self::Default, -            Position::Centered => Self::Centered, -            Position::Specific(x, y) => Self::Specific(x, y), -        } -    } -} diff --git a/src/window/settings.rs b/src/window/settings.rs deleted file mode 100644 index 24d0f4f9..00000000 --- a/src/window/settings.rs +++ /dev/null @@ -1,70 +0,0 @@ -use crate::window::{Icon, Position}; - -/// The window settings of an application. -#[derive(Debug, Clone)] -pub struct Settings { -    /// The initial size of the window. -    pub size: (u32, u32), - -    /// The initial position of the window. -    pub position: Position, - -    /// The minimum size of the window. -    pub min_size: Option<(u32, u32)>, - -    /// The maximum size of the window. -    pub max_size: Option<(u32, u32)>, - -    /// Whether the window should be visible or not. -    pub visible: bool, - -    /// Whether the window should be resizable or not. -    pub resizable: bool, - -    /// Whether the window should have a border, a title bar, etc. or not. -    pub decorations: bool, - -    /// Whether the window should be transparent. -    pub transparent: bool, - -    /// Whether the window will always be on top of other windows. -    pub always_on_top: bool, - -    /// The icon of the window. -    pub icon: Option<Icon>, -} - -impl Default for Settings { -    fn default() -> Settings { -        Settings { -            size: (1024, 768), -            position: Position::default(), -            min_size: None, -            max_size: None, -            visible: true, -            resizable: true, -            decorations: true, -            transparent: false, -            always_on_top: false, -            icon: None, -        } -    } -} - -impl From<Settings> for iced_winit::settings::Window { -    fn from(settings: Settings) -> Self { -        Self { -            size: settings.size, -            position: iced_winit::Position::from(settings.position), -            min_size: settings.min_size, -            max_size: settings.max_size, -            visible: settings.visible, -            resizable: settings.resizable, -            decorations: settings.decorations, -            transparent: settings.transparent, -            always_on_top: settings.always_on_top, -            icon: settings.icon.map(Icon::into), -            platform_specific: Default::default(), -        } -    } -} | 
