Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(renderer): definable default color for symbolic icons #59

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"]

[features]
default = ["winit", "a11y"]
# Enable the `wgpu` GPU-accelerated renderer backend
wgpu = ["iced_renderer/wgpu"]
# Enables the `Image` widget
Expand Down
3 changes: 3 additions & 0 deletions core/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct Quad {
/// The styling attributes of a [`Renderer`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
/// The color to apply to symbolic icons.
pub icon_color: Color,
/// The text color
pub text_color: Color,
/// The scale factor
Expand All @@ -72,6 +74,7 @@ pub struct Style {
impl Default for Style {
fn default() -> Self {
Style {
icon_color: Color::BLACK,
text_color: Color::BLACK,
scale_factor: 1.0,
}
Expand Down
23 changes: 16 additions & 7 deletions sctk/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ where
configure.new_size.1 as f64,
);
}

}
}
LayerSurfaceEventVariant::ScaleFactorChanged(sf, viewport) => {
Expand Down Expand Up @@ -854,6 +854,7 @@ where
&mut renderer,
state.theme(),
&Style {
icon_color: state.icon_color(),
text_color: state.text_color(),
scale_factor: state.scale_factor(),
},
Expand Down Expand Up @@ -1012,10 +1013,11 @@ where
has_events || !native_events.is_empty();

let (interface_state, statuses) = {
let Some(user_interface) = interfaces
.get_mut(&surface_id.inner()) else {
continue;
};
let Some(user_interface) =
interfaces.get_mut(&surface_id.inner())
else {
continue;
};
user_interface.update(
native_events.as_slice(),
cursor_position,
Expand Down Expand Up @@ -1141,8 +1143,9 @@ where
Instant::now(),
),
);
let Some(user_interface) = interfaces
.get_mut(&surface_id.inner()) else {
let Some(user_interface) =
interfaces.get_mut(&surface_id.inner())
else {
continue;
};
let (interface_state, _) = user_interface.update(
Expand Down Expand Up @@ -1306,6 +1309,7 @@ where
&mut renderer,
state.theme(),
&Style {
icon_color: state.icon_color(),
text_color: state.text_color(),
scale_factor: state.scale_factor(),
},
Expand Down Expand Up @@ -1725,6 +1729,11 @@ where
self.appearance.background_color
}

/// Returns the current icon [`Color`] of the [`State`].
pub fn icon_color(&self) -> Color {
self.appearance.icon_color
}

/// Returns the current text [`Color`] of the [`State`].
pub fn text_color(&self) -> Color {
self.appearance.text_color
Expand Down
7 changes: 1 addition & 6 deletions sctk/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ use sctk::{
},
registry::RegistryState,
seat::SeatState,
shell::{
wlr_layer::LayerShell,
xdg::XdgShell,
WaylandSurface,
},
shell::{wlr_layer::LayerShell, xdg::XdgShell, WaylandSurface},
shm::Shm,
};
#[cfg(feature = "a11y")]
Expand Down Expand Up @@ -680,7 +676,6 @@ where
let wl_surface = layer_surface.surface.wl_surface();

if let Some(mut prev_configure) = layer_surface.last_configure.clone() {

prev_configure.new_size = (width.unwrap_or(prev_configure.new_size.0), width.unwrap_or(prev_configure.new_size.1));
sticky_exit_callback(
IcedSctkEvent::SctkEvent(SctkEvent::LayerSurfaceEvent { variant: LayerSurfaceEventVariant::Configure(prev_configure, wl_surface.clone(), false), id: wl_surface.clone()}),
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@
#![allow(clippy::inherent_to_string, clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(all(not(feature = "wayland"), not(feature = "winit")))]
compile_error!("must define `wayland` or `winit` feature");

#[cfg(all(feature = "wayland", feature = "winit"))]
compile_error!("cannot use `wayland` feature with `winit");

pub use iced_futures::futures;
use iced_widget::graphics;
use iced_widget::renderer;
Expand Down
3 changes: 3 additions & 0 deletions style/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct Appearance {
/// The background [`Color`] of the application.
pub background_color: Color,

/// The default icon [`Color`] of the application.
pub icon_color: Color,

/// The default text [`Color`] of the application.
pub text_color: Color,
}
3 changes: 3 additions & 0 deletions style/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct Appearance {
pub border_width: f32,
/// The border [`Color`] of the button.
pub border_color: Color,
/// The icon [`Color`] of the button.
pub icon_color: Option<Color>,
/// The text [`Color`] of the button.
pub text_color: Color,
}
Expand All @@ -26,6 +28,7 @@ impl std::default::Default for Appearance {
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
icon_color: None,
text_color: Color::BLACK,
}
}
Expand Down
3 changes: 3 additions & 0 deletions style/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use iced_core::{Background, BorderRadius, Color};
/// The appearance of a container.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The icon [`Color`] of the container.
pub icon_color: Option<Color>,
/// The text [`Color`] of the container.
pub text_color: Option<Color>,
/// The [`Background`] of the container.
Expand All @@ -19,6 +21,7 @@ pub struct Appearance {
impl std::default::Default for Appearance {
fn default() -> Self {
Self {
icon_color: None,
text_color: None,
background: None,
border_radius: 0.0.into(),
Expand Down
2 changes: 2 additions & 0 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl application::StyleSheet for Theme {
match style {
Application::Default => application::Appearance {
background_color: palette.background.base.color,
icon_color: palette.background.base.icon,
text_color: palette.background.base.text,
},
Application::Custom(custom) => custom.appearance(self),
Expand Down Expand Up @@ -381,6 +382,7 @@ impl container::StyleSheet for Theme {
let palette = self.extended_palette();

container::Appearance {
icon_color: None,
text_color: None,
background: Some(palette.background.weak.color.into()),
border_radius: 2.0.into(),
Expand Down
10 changes: 8 additions & 2 deletions style/src/theme/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ impl Extended {
}
}

/// A pair of background and text colors.
/// Recommended background, icon, and text [`Color`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pair {
/// The background color.
pub color: Color,

/// The icon color, which defaults to the text color.
pub icon: Color,

/// The text color.
///
/// It's guaranteed to be readable on top of the background [`color`].
Expand All @@ -134,9 +137,12 @@ pub struct Pair {
impl Pair {
/// Creates a new [`Pair`] from a background [`Color`] and some text [`Color`].
pub fn new(color: Color, text: Color) -> Self {
let text = readable(color, text);

Self {
color,
text: readable(color, text),
icon: text,
text,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions widget/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ where
renderer,
theme,
&renderer::Style {
icon_color: styling
.icon_color
.unwrap_or(renderer_style.icon_color),
text_color: styling.text_color,
scale_factor: renderer_style.scale_factor,
},
Expand Down
3 changes: 3 additions & 0 deletions widget/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ where
renderer,
theme,
&renderer::Style {
icon_color: style
.icon_color
.unwrap_or(renderer_style.icon_color),
text_color: style
.text_color
.unwrap_or(renderer_style.text_color),
Expand Down
42 changes: 41 additions & 1 deletion widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A container for capturing mouse events.

use iced_renderer::core::widget::OperationOutputWrapper;
use iced_renderer::core::Point;

use crate::core::event::{self, Event};
use crate::core::layout;
Expand All @@ -17,6 +18,7 @@ use crate::core::{
#[allow(missing_debug_implementations)]
pub struct MouseArea<'a, Message, Renderer> {
content: Element<'a, Message, Renderer>,
on_drag: Option<Message>,
on_press: Option<Message>,
on_release: Option<Message>,
on_right_press: Option<Message>,
Expand All @@ -26,6 +28,13 @@ pub struct MouseArea<'a, Message, Renderer> {
}

impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
/// The message to emit when a drag is initiated.
#[must_use]
pub fn on_drag(mut self, message: Message) -> Self {
self.on_drag = Some(message);
self
}

/// The message to emit on a left button press.
#[must_use]
pub fn on_press(mut self, message: Message) -> Self {
Expand Down Expand Up @@ -73,13 +82,15 @@ impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
#[derive(Default)]
struct State {
// TODO: Support on_mouse_enter and on_mouse_exit
drag_initiated: Option<Point>,
}

impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
/// Creates a [`MouseArea`] with the given content.
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
MouseArea {
content: content.into(),
on_drag: None,
on_press: None,
on_release: None,
on_right_press: None,
Expand Down Expand Up @@ -167,7 +178,14 @@ where
return event::Status::Captured;
}

update(self, &event, layout, cursor, shell)
update(
self,
&event,
layout,
cursor,
shell,
tree.state.downcast_mut::<State>(),
)
}

fn mouse_interaction(
Expand Down Expand Up @@ -243,6 +261,7 @@ fn update<Message: Clone, Renderer>(
layout: Layout<'_>,
cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>,
state: &mut State,
) -> event::Status {
if !cursor.is_over(layout.bounds()) {
return event::Status::Ignored;
Expand All @@ -252,6 +271,7 @@ fn update<Message: Clone, Renderer>(
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) = event
{
state.drag_initiated = cursor.position();
shell.publish(message.clone());

return event::Status::Captured;
Expand All @@ -262,6 +282,7 @@ fn update<Message: Clone, Renderer>(
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(touch::Event::FingerLifted { .. }) = event
{
state.drag_initiated = None;
shell.publish(message.clone());

return event::Status::Captured;
Expand Down Expand Up @@ -311,5 +332,24 @@ fn update<Message: Clone, Renderer>(
}
}

if state.drag_initiated.is_none() && widget.on_drag.is_some() {
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) = event
{
state.drag_initiated = cursor.position();
}
} else if let Some((message, drag_source)) =
widget.on_drag.as_ref().zip(state.drag_initiated)
{
if let Some(position) = cursor.position() {
if position.distance(drag_source) > 1.0 {
state.drag_initiated = None;
shell.publish(message.clone());

return event::Status::Captured;
}
}
}

event::Status::Ignored
}
1 change: 1 addition & 0 deletions widget/src/pane_grid/title_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ where
let bounds = layout.bounds();
let style = theme.appearance(&self.style);
let inherited_style = renderer::Style {
icon_color: style.icon_color.unwrap_or(inherited_style.icon_color),
text_color: style.text_color.unwrap_or(inherited_style.text_color),
scale_factor: inherited_style.scale_factor,
};
Expand Down
Loading
Loading