Skip to content

Commit

Permalink
refactor: include theme in state
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Oct 16, 2023
1 parent 2b3b6d9 commit 5f466fb
Show file tree
Hide file tree
Showing 21 changed files with 296 additions and 154 deletions.
3 changes: 2 additions & 1 deletion src/backend/kms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,10 +1235,11 @@ impl Surface {
})?;
self.fps.elements();

let theme = state.theme.cosmic();
let res = compositor.render_frame::<_, _, GlesTexture>(
&mut renderer,
&elements,
crate::theme::clear_color(),
crate::theme::clear_color(&theme),
);
self.fps.render();

Expand Down
27 changes: 17 additions & 10 deletions src/backend/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
},
};

use cosmic::cosmic_theme::palette::white_point::C;
use cosmic_protocols::screencopy::v1::server::zcosmic_screencopy_session_v1::FailureReason;
use keyframe::{ease, functions::EaseInOutCubic};
use smithay::{
Expand Down Expand Up @@ -159,12 +160,12 @@ impl IndicatorShader {
thickness: u8,
scale: f64,
alpha: f32,
active_window_hint: [f32; 3],
) -> PixelShaderElement {
let t = thickness as i32;
element_geo.loc -= (t, t).into();
element_geo.size += (t * 2, t * 2).into();

let focus_indicator = crate::theme::active_window_hint();
IndicatorShader::element(
renderer,
key,
Expand All @@ -173,11 +174,7 @@ impl IndicatorShader {
thickness * 2,
alpha,
scale,
[
focus_indicator.red,
focus_indicator.green,
focus_indicator.blue,
],
active_window_hint,
)
}

Expand Down Expand Up @@ -413,13 +410,14 @@ where
);
}

let theme = state.theme.cosmic();
if let Some(grab_elements) = seat
.user_data()
.get::<SeatMoveGrabState>()
.unwrap()
.borrow()
.as_ref()
.map(|state| state.render::<E, R>(renderer, seat, output))
.map(|state| state.render::<E, R>(renderer, seat, output, theme))
{
elements.extend(grab_elements);
}
Expand Down Expand Up @@ -449,6 +447,7 @@ where
#[cfg(feature = "debug")]
puffin::profile_function!();

let theme = state.theme.cosmic();
let mut elements = cursor_elements(renderer, state, output, cursor_mode);

#[cfg(feature = "debug")]
Expand Down Expand Up @@ -540,6 +539,8 @@ where
elements.extend(overlay_popups.into_iter().map(Into::into));
elements.extend(overlay_elements.into_iter().map(Into::into));

let active_hint = state.theme.cosmic().active_hint as u8;

let mut window_elements = if !has_fullscreen.unwrap_or(false) {
let (top_elements, top_popups) =
split_layer_elements(renderer, output, Layer::Top, exclude_workspace_overview);
Expand Down Expand Up @@ -580,7 +581,6 @@ where
}
});

let active_hint = crate::theme::active_hint();
let (w_elements, p_elements) = workspace
.render_output::<R>(
renderer,
Expand All @@ -591,6 +591,7 @@ where
overview.clone(),
resize_indicator.clone(),
active_hint,
theme,
)
.map_err(|_| OutputNoMode)?;
elements.extend(p_elements.into_iter().map(|p_element| {
Expand Down Expand Up @@ -648,7 +649,8 @@ where
(!move_active && is_active_space).then_some(&last_active_seat),
overview,
resize_indicator,
crate::theme::active_hint(),
active_hint,
theme,
)
.map_err(|_| OutputNoMode)?;
elements.extend(p_elements.into_iter().map(|p_element| {
Expand Down Expand Up @@ -920,7 +922,12 @@ where
}

renderer.bind(target).map_err(RenderError::Rendering)?;
let res = damage_tracker.render_output(renderer, age, &elements, crate::theme::clear_color());
let res = damage_tracker.render_output(
renderer,
age,
&elements,
crate::theme::clear_color(state.theme.cosmic()),
);

if let Some(fps) = fps.as_mut() {
fps.render();
Expand Down
14 changes: 11 additions & 3 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
wayland::{handlers::screencopy::ScreencopySessions, protocols::screencopy::Session},
};
use calloop::{timer::Timer, RegistrationToken};
use cosmic::theme::CosmicTheme;
use cosmic_protocols::screencopy::v1::server::zcosmic_screencopy_session_v1::InputType;
#[allow(deprecated)]
use smithay::{
Expand Down Expand Up @@ -214,7 +215,8 @@ impl State {
<B as InputBackend>::PointerAxisEvent: 'static,
{
use smithay::backend::input::Event;

let theme_ = self.common.theme.clone();
let theme = theme_.cosmic();
match event {
InputEvent::DeviceAdded { device } => {
let seat = &mut self.common.last_active_seat();
Expand Down Expand Up @@ -570,6 +572,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0.clone(),
workspace,
theme,
);

let ptr = seat.get_pointer().unwrap();
Expand Down Expand Up @@ -638,6 +641,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0,
workspace,
theme,
);

position.x = position.x.clamp(
Expand Down Expand Up @@ -766,6 +770,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0,
workspace,
theme,
);

for session in sessions_for_output(&self.common, &output) {
Expand Down Expand Up @@ -888,7 +893,7 @@ impl State {
under = Some(surface.clone().into());
} else {
if let Some((target, _)) =
workspace.element_under(relative_pos, overview.0)
workspace.element_under(relative_pos, overview.0, theme)
{
under = Some(target);
} else {
Expand Down Expand Up @@ -1629,6 +1634,7 @@ impl State {
&window,
&current_output,
self.common.event_loop_handle.clone(),
self.common.theme.clone(),
);
}
}
Expand Down Expand Up @@ -1712,6 +1718,7 @@ impl State {
override_redirect_windows: &[X11Surface],
overview: OverviewMode,
workspace: &mut Workspace,
theme: &CosmicTheme,
) -> Option<(PointerFocusTarget, Point<i32, Logical>)> {
if let Some(window) = workspace.get_fullscreen(output) {
let layers = layer_map_for_output(output);
Expand Down Expand Up @@ -1757,7 +1764,8 @@ impl State {
let offset = layer_map_for_output(output).non_exclusive_zone().loc;
return Some((surface.clone().into(), output_geo.loc + offset));
} else {
if let Some((target, loc)) = workspace.element_under(relative_pos, overview) {
if let Some((target, loc)) = workspace.element_under(relative_pos, overview, theme)
{
return Some((target, loc + (global_pos - relative_pos).to_i32_round()));
}
{
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ fn main() -> Result<()> {
// potentially tell the session we are setup now
session::setup_socket(event_loop.handle(), &state)?;

let _theme_res = theme::watch_theme(event_loop.handle());
if let Err(err) = theme::watch_theme(event_loop.handle()) {
warn!(?err, "Failed to watch theme");
}

// run the event loop
event_loop.run(None, &mut state, |state| {
Expand Down
12 changes: 7 additions & 5 deletions src/shell/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,15 @@ impl CosmicMapped {
pub fn convert_to_stack<'a>(
&mut self,
outputs: impl Iterator<Item = (&'a Output, Rectangle<i32, Logical>)>,
theme: cosmic::Theme,
) {
match &self.element {
CosmicMappedInternal::Window(window) => {
let surface = window.surface();
let activated = surface.is_activated(true);
let handle = window.loop_handle();

let stack = CosmicStack::new(std::iter::once(surface), handle);
let stack = CosmicStack::new(std::iter::once(surface), handle, theme);
if let Some(geo) = self.last_geometry.lock().unwrap().clone() {
stack.set_geometry(geo);
}
Expand All @@ -520,11 +521,12 @@ impl CosmicMapped {
&mut self,
surface: CosmicSurface,
outputs: impl Iterator<Item = (&'a Output, Rectangle<i32, Logical>)>,
theme: cosmic::Theme,
) {
let handle = self.loop_handle();
surface.try_force_undecorated(false);
surface.set_tiled(false);
let window = CosmicWindow::new(surface, handle);
let window = CosmicWindow::new(surface, handle, theme);

if let Some(geo) = self.last_geometry.lock().unwrap().clone() {
window.set_geometry(geo);
Expand Down Expand Up @@ -760,10 +762,10 @@ impl CosmicMapped {
)
}

pub(crate) fn set_theme(&self) {
pub(crate) fn update_theme(&self, theme: cosmic::Theme) {
match &self.element {
CosmicMappedInternal::Window(w) => w.set_theme(),
CosmicMappedInternal::Stack(s) => s.set_theme(),
CosmicMappedInternal::Window(w) => w.set_theme(theme),
CosmicMappedInternal::Stack(s) => s.set_theme(theme),
CosmicMappedInternal::_GenericCatcher(_) => {}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/shell/element/resize_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn resize_indicator(
direction: ResizeDirection,
config: &Config,
evlh: LoopHandle<'static, crate::state::State>,
theme: cosmic::Theme,
) -> ResizeIndicator {
ResizeIndicator::new(
ResizeIndicatorInternal {
Expand All @@ -49,6 +50,7 @@ pub fn resize_indicator(
},
Size::from((1, 1)),
evlh,
theme,
)
}

Expand Down
10 changes: 7 additions & 3 deletions src/shell/element/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl CosmicStack {
pub fn new<I: Into<CosmicSurface>>(
windows: impl Iterator<Item = I>,
handle: LoopHandle<'static, crate::state::State>,
theme: cosmic::Theme,
) -> CosmicStack {
let windows = windows.map(Into::into).collect::<Vec<_>>();
assert!(!windows.is_empty());
Expand Down Expand Up @@ -160,6 +161,7 @@ impl CosmicStack {
},
(width, TAB_HEIGHT),
handle,
theme,
))
}

Expand Down Expand Up @@ -545,8 +547,8 @@ impl CosmicStack {
)
}

pub(crate) fn set_theme(&self) {
self.0.set_theme();
pub(crate) fn set_theme(&self, theme: cosmic::Theme) {
self.0.set_theme(theme);
}

pub(crate) fn force_redraw(&self) {
Expand Down Expand Up @@ -1064,10 +1066,12 @@ impl PointerTarget<State> for CosmicStack {
let mapped = CosmicMapped::from(CosmicWindow::new(
surface,
self.0.loop_handle(),
data.common.theme.clone(),
));
let elem_geo =
workspace.element_geometry(stack_mapped).unwrap();
let indicator_thickness = crate::theme::active_hint();
let indicator_thickness =
data.common.theme.cosmic().active_hint as u8;
let was_tiled = workspace.is_tiled(stack_mapped);

self.remove_idx(dragged_out);
Expand Down
3 changes: 2 additions & 1 deletion src/shell/element/stack_hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ pub type StackHover = IcedElement<StackHoverInternal>;
pub fn stack_hover(
evlh: LoopHandle<'static, crate::state::State>,
size: Size<i32, Logical>,
theme: cosmic::Theme,
) -> StackHover {
StackHover::new(StackHoverInternal, size, evlh)
StackHover::new(StackHoverInternal, size, evlh, theme)
}

pub struct StackHoverInternal;
Expand Down
7 changes: 5 additions & 2 deletions src/shell/element/swap_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use smithay::utils::Size;

pub type SwapIndicator = IcedElement<SwapIndicatorInternal>;

pub fn swap_indicator(evlh: LoopHandle<'static, crate::state::State>) -> SwapIndicator {
SwapIndicator::new(SwapIndicatorInternal, Size::from((1, 1)), evlh)
pub fn swap_indicator(
evlh: LoopHandle<'static, crate::state::State>,
theme: cosmic::Theme,
) -> SwapIndicator {
SwapIndicator::new(SwapIndicatorInternal, Size::from((1, 1)), evlh, theme)
}

pub struct SwapIndicatorInternal;
Expand Down
7 changes: 5 additions & 2 deletions src/shell/element/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl CosmicWindow {
pub fn new(
window: impl Into<CosmicSurface>,
handle: LoopHandle<'static, crate::state::State>,
theme: cosmic::Theme,
) -> CosmicWindow {
let window = window.into();
let width = window.geometry().size.w;
Expand All @@ -129,6 +130,7 @@ impl CosmicWindow {
},
(width, SSD_HEIGHT),
handle,
theme,
))
}

Expand Down Expand Up @@ -214,8 +216,8 @@ impl CosmicWindow {
)
}

pub(crate) fn set_theme(&self) {
self.0.set_theme();
pub(crate) fn set_theme(&self, theme: cosmic::Theme) {
self.0.set_theme(theme);
}

pub(crate) fn force_redraw(&self) {
Expand Down Expand Up @@ -265,6 +267,7 @@ impl Program for CosmicWindowInternal {
&window,
&output,
state.common.event_loop_handle.clone(),
state.common.theme.clone(),
)
}
}
Expand Down
Loading

0 comments on commit 5f466fb

Please sign in to comment.