cosmic-comp/src/shell/element/resize_indicator.rs

197 lines
6.6 KiB
Rust
Raw Normal View History

2023-07-06 00:03:26 +02:00
use std::sync::Mutex;
use crate::{
config::{Action, Config},
fl,
shell::{grabs::ResizeEdge, ResizeDirection},
utils::iced::{IcedElement, Program},
};
use calloop::LoopHandle;
use cosmic::{
iced::widget::{column, container, horizontal_space, row, vertical_space},
iced_core::{Background, Color, Length},
theme,
2023-10-02 19:37:23 +02:00
widget::{icon::from_name, text},
Apply,
2023-07-06 00:03:26 +02:00
};
use smithay::utils::Size;
pub type ResizeIndicator = IcedElement<ResizeIndicatorInternal>;
pub fn resize_indicator(
direction: ResizeDirection,
config: &Config,
2023-09-29 21:33:16 +02:00
evlh: LoopHandle<'static, crate::state::State>,
2023-07-06 00:03:26 +02:00
) -> ResizeIndicator {
ResizeIndicator::new(
ResizeIndicatorInternal {
edges: Mutex::new(ResizeEdge::all()),
direction,
shortcut1: config
.static_conf
.key_bindings
.iter()
.find_map(|(pattern, action)| {
(*action == Action::Resizing(ResizeDirection::Outwards)).then_some(pattern)
})
.map(|pattern| format!("{}: ", pattern.to_string()))
.unwrap_or_else(|| crate::fl!("unknown-keybinding")),
shortcut2: config
.static_conf
.key_bindings
.iter()
.find_map(|(pattern, action)| {
(*action == Action::Resizing(ResizeDirection::Inwards)).then_some(pattern)
})
.map(|pattern| format!("{}: ", pattern.to_string()))
.unwrap_or_else(|| crate::fl!("unknown-keybinding")),
},
Size::from((1, 1)),
evlh,
)
}
pub struct ResizeIndicatorInternal {
pub edges: Mutex<ResizeEdge>,
pub direction: ResizeDirection,
pub shortcut1: String,
pub shortcut2: String,
}
impl Program for ResizeIndicatorInternal {
type Message = ();
fn view(&self) -> crate::utils::iced::Element<'_, Self::Message> {
let edges = self.edges.lock().unwrap();
2023-10-02 19:37:23 +02:00
let icon_container_style = || {
theme::Container::custom(|theme| container::Appearance {
icon_color: Some(Color::from(theme.cosmic().accent.on)),
text_color: Some(Color::from(theme.cosmic().accent.on)),
background: Some(Background::Color(theme.cosmic().accent_color().into())),
border_radius: 18.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
})
};
2023-07-06 00:03:26 +02:00
column(vec![
if edges.contains(ResizeEdge::TOP) {
2023-10-02 19:37:23 +02:00
from_name(if self.direction == ResizeDirection::Outwards {
"go-up-symbolic"
} else {
"go-down-symbolic"
})
.size(32)
.prefer_svg(true)
2023-07-06 00:03:26 +02:00
.apply(container)
.padding(2)
2023-10-02 19:37:23 +02:00
.style(icon_container_style())
2023-07-06 00:03:26 +02:00
.width(Length::Shrink)
.apply(container)
.center_x()
.width(Length::Fill)
.into()
} else {
vertical_space(36).into()
},
row(vec![
if edges.contains(ResizeEdge::LEFT) {
2023-10-02 19:37:23 +02:00
from_name(if self.direction == ResizeDirection::Outwards {
"go-previous-symbolic"
} else {
"go-next-symbolic"
})
.size(32)
.prefer_svg(true)
2023-07-06 00:03:26 +02:00
.apply(container)
.padding(4)
2023-10-02 19:37:23 +02:00
.style(icon_container_style())
2023-07-06 00:03:26 +02:00
.width(Length::Shrink)
.apply(container)
.center_y()
.height(Length::Fill)
.into()
} else {
horizontal_space(36).into()
},
row(vec![
text(&self.shortcut1)
.font(cosmic::font::FONT_SEMIBOLD)
.size(14)
.into(),
text(fl!("grow-window"))
.font(cosmic::font::FONT)
.size(14)
.into(),
horizontal_space(40).into(),
text(&self.shortcut2)
.font(cosmic::font::FONT_SEMIBOLD)
.size(14)
.into(),
text(fl!("shrink-window"))
.font(cosmic::font::FONT)
.size(14)
.into(),
])
.apply(container)
.center_x()
.center_y()
.padding(16)
.apply(container)
2023-10-02 19:37:23 +02:00
.style(icon_container_style())
2023-07-06 00:03:26 +02:00
.width(Length::Shrink)
.height(Length::Shrink)
.apply(container)
.height(Length::Fill)
.width(Length::Fill)
.center_x()
.center_y()
.into(),
if edges.contains(ResizeEdge::RIGHT) {
2023-10-02 19:37:23 +02:00
from_name(if self.direction == ResizeDirection::Outwards {
"go-next-symbolic"
} else {
"go-previous-symbolic"
})
.size(32)
.prefer_svg(true)
2023-07-06 00:03:26 +02:00
.apply(container)
.padding(4)
2023-10-02 19:37:23 +02:00
.style(icon_container_style())
2023-07-06 00:03:26 +02:00
.height(Length::Shrink)
.apply(container)
.center_y()
.height(Length::Fill)
.into()
} else {
horizontal_space(36).into()
},
])
.width(Length::Fill)
.height(Length::Fill)
.into(),
if edges.contains(ResizeEdge::BOTTOM) {
2023-10-02 19:37:23 +02:00
from_name(if self.direction == ResizeDirection::Outwards {
"go-down-symbolic"
} else {
"go-up-symbolic"
})
.size(32)
.prefer_svg(true)
2023-07-06 00:03:26 +02:00
.apply(container)
.padding(4)
2023-10-02 19:37:23 +02:00
.style(icon_container_style())
2023-07-06 00:03:26 +02:00
.width(Length::Shrink)
.apply(container)
.center_x()
.width(Length::Fill)
.into()
} else {
vertical_space(36).into()
},
])
.into()
}
}