use super::{AppBarButton, AppBarSeparator, AppBarToggleButton};
use crate::InputDevice;
use waft_foundation::Listener;
use waft_widgets::FocusNodeRef;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CommandBarDefaultLabelPosition {
#[default]
Bottom,
Right,
Collapsed,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CommandBarLabelPosition {
#[default]
Default,
Collapsed,
}
#[derive(Clone, Debug)]
pub enum CommandBarElement {
Button(AppBarButton),
ToggleButton(AppBarToggleButton),
Separator(AppBarSeparator),
}
impl From<AppBarButton> for CommandBarElement {
fn from(button: AppBarButton) -> Self {
CommandBarElement::Button(button)
}
}
impl From<AppBarToggleButton> for CommandBarElement {
fn from(button: AppBarToggleButton) -> Self {
CommandBarElement::ToggleButton(button)
}
}
impl From<AppBarSeparator> for CommandBarElement {
fn from(separator: AppBarSeparator) -> Self {
CommandBarElement::Separator(separator)
}
}
impl CommandBarElement {
pub fn dynamic_overflow_order(&self) -> i32 {
match self {
CommandBarElement::Button(button) => button.dynamic_overflow_order,
CommandBarElement::ToggleButton(button) => button.dynamic_overflow_order,
CommandBarElement::Separator(separator) => separator.dynamic_overflow_order,
}
}
pub(crate) fn is_separator(&self) -> bool {
matches!(self, CommandBarElement::Separator(_))
}
pub(crate) fn label(&self) -> Option<&str> {
match self {
CommandBarElement::Button(button) => Some(&button.label),
CommandBarElement::ToggleButton(button) => Some(&button.label),
CommandBarElement::Separator(_) => None,
}
}
pub(crate) fn has_icon(&self) -> bool {
match self {
CommandBarElement::Button(button) => button.icon.is_some(),
CommandBarElement::ToggleButton(button) => button.icon.is_some(),
CommandBarElement::Separator(_) => false,
}
}
pub(crate) fn keyboard_accelerator_text(&self) -> &str {
match self {
CommandBarElement::Button(button) => &button.keyboard_accelerator_text_override,
CommandBarElement::ToggleButton(button) => &button.keyboard_accelerator_text_override,
CommandBarElement::Separator(_) => "",
}
}
pub(crate) fn is_focusable(&self) -> bool {
match self {
CommandBarElement::Button(button) => button.is_enabled,
CommandBarElement::ToggleButton(button) => button.is_enabled,
CommandBarElement::Separator(_) => false,
}
}
pub(crate) fn has_label_at(
&self,
default: CommandBarDefaultLabelPosition,
position: CommandBarDefaultLabelPosition,
) -> bool {
let (label, label_position) = match self {
CommandBarElement::Button(button) => (&button.label, button.label_position),
CommandBarElement::ToggleButton(button) => (&button.label, button.label_position),
CommandBarElement::Separator(_) => return false,
};
effective_label_position(label_position, default) == position && !label.is_empty()
}
pub(crate) fn hosted(
&self,
presentation: ElementPresentation,
host: ElementHost,
) -> CommandBarElement {
match self.clone() {
CommandBarElement::Button(mut button) => {
button.presentation = presentation;
button.host = Some(host);
CommandBarElement::Button(button)
}
CommandBarElement::ToggleButton(mut button) => {
button.presentation = presentation;
button.host = Some(host);
CommandBarElement::ToggleButton(button)
}
CommandBarElement::Separator(mut separator) => {
separator.presentation = presentation;
CommandBarElement::Separator(separator)
}
}
}
pub(crate) fn signature(&self) -> (u8, Option<String>, i32) {
let kind = match self {
CommandBarElement::Button(_) => 0,
CommandBarElement::ToggleButton(_) => 1,
CommandBarElement::Separator(_) => 2,
};
(
kind,
self.label().map(str::to_string),
self.dynamic_overflow_order(),
)
}
}
pub(crate) fn effective_label_position(
label_position: CommandBarLabelPosition,
default: CommandBarDefaultLabelPosition,
) -> CommandBarDefaultLabelPosition {
match label_position {
CommandBarLabelPosition::Collapsed => CommandBarDefaultLabelPosition::Collapsed,
CommandBarLabelPosition::Default => default,
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct ElementPresentation {
pub(crate) is_compact: bool,
pub(crate) use_overflow_style: bool,
pub(crate) default_label_position: CommandBarDefaultLabelPosition,
pub(crate) with_icons: bool,
pub(crate) with_toggle_buttons: bool,
pub(crate) with_keyboard_accelerator_text: bool,
pub(crate) input_mode: InputDevice,
pub(crate) keyboard_accelerator_text_min_width: f64,
pub(crate) in_command_bar: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ApplicationViewState {
FullSize,
Compact,
LabelOnRight,
LabelCollapsed,
Overflow,
OverflowWithToggleButtons,
OverflowWithMenuIcons,
OverflowWithToggleButtonsAndMenuIcons,
}
impl ApplicationViewState {
pub(crate) fn of(
presentation: &ElementPresentation,
label_position: CommandBarLabelPosition,
) -> ApplicationViewState {
if presentation.use_overflow_style {
return match (presentation.with_toggle_buttons, presentation.with_icons) {
(true, true) => ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons,
(true, false) => ApplicationViewState::OverflowWithToggleButtons,
(false, true) => ApplicationViewState::OverflowWithMenuIcons,
(false, false) => ApplicationViewState::Overflow,
};
}
match effective_label_position(label_position, presentation.default_label_position) {
CommandBarDefaultLabelPosition::Right => ApplicationViewState::LabelOnRight,
CommandBarDefaultLabelPosition::Collapsed => ApplicationViewState::LabelCollapsed,
CommandBarDefaultLabelPosition::Bottom if presentation.is_compact => {
ApplicationViewState::Compact
}
CommandBarDefaultLabelPosition::Bottom => ApplicationViewState::FullSize,
}
}
pub(crate) fn is_overflow(self) -> bool {
matches!(
self,
ApplicationViewState::Overflow
| ApplicationViewState::OverflowWithToggleButtons
| ApplicationViewState::OverflowWithMenuIcons
| ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons
)
}
}
#[derive(Clone, Debug)]
pub(crate) struct ElementHost {
pub(crate) executed: Listener,
pub(crate) focus_node: FocusNodeRef,
}