Remove unescessary enum

This commit is contained in:
Mikayla Maki 2023-06-20 15:46:30 -07:00
parent 60b4054b0a
commit dacfd70fb4
No known key found for this signature in database
3 changed files with 12 additions and 48 deletions

View file

@ -9,7 +9,6 @@ use gpui::{
}; };
use menu::*; use menu::*;
use std::{any::TypeId, borrow::Cow, sync::Arc, time::Duration}; use std::{any::TypeId, borrow::Cow, sync::Arc, time::Duration};
use theme::ToggleState;
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.add_action(ContextMenu::select_first); cx.add_action(ContextMenu::select_first);
@ -329,12 +328,7 @@ impl ContextMenu {
Flex::column().with_children(self.items.iter().enumerate().map(|(ix, item)| { Flex::column().with_children(self.items.iter().enumerate().map(|(ix, item)| {
match item { match item {
ContextMenuItem::Item { label, .. } => { ContextMenuItem::Item { label, .. } => {
let toggle_state = if Some(ix) == self.selected_index { let style = style.item.in_state(self.selected_index == Some(ix));
ToggleState::Active
} else {
ToggleState::Inactive
};
let style = style.item.in_state(toggle_state);
let style = style.style_for(&mut Default::default()); let style = style.style_for(&mut Default::default());
match label { match label {
@ -367,12 +361,7 @@ impl ContextMenu {
.with_children(self.items.iter().enumerate().map(|(ix, item)| { .with_children(self.items.iter().enumerate().map(|(ix, item)| {
match item { match item {
ContextMenuItem::Item { action, .. } => { ContextMenuItem::Item { action, .. } => {
let toggle_state = if Some(ix) == self.selected_index { let style = style.item.in_state(self.selected_index == Some(ix));
ToggleState::Active
} else {
ToggleState::Inactive
};
let style = style.item.in_state(toggle_state);
let style = style.style_for(&mut Default::default()); let style = style.style_for(&mut Default::default());
match action { match action {
@ -419,12 +408,7 @@ impl ContextMenu {
let action = action.clone(); let action = action.clone();
let view_id = self.parent_view_id; let view_id = self.parent_view_id;
MouseEventHandler::<MenuItem, ContextMenu>::new(ix, cx, |state, _| { MouseEventHandler::<MenuItem, ContextMenu>::new(ix, cx, |state, _| {
let toggle_state = if Some(ix) == self.selected_index { let style = style.item.in_state(self.selected_index == Some(ix));
ToggleState::Active
} else {
ToggleState::Inactive
};
let style = style.item.in_state(toggle_state);
let style = style.style_for(state); let style = style.style_for(state);
let keystroke = match &action { let keystroke = match &action {
ContextMenuItemAction::Action(action) => Some( ContextMenuItemAction::Action(action) => Some(

View file

@ -816,37 +816,22 @@ pub struct Toggleable<T> {
inactive: T, inactive: T,
} }
#[derive(Copy, Clone, Debug, Default, Hash, PartialEq, Eq)]
pub enum ToggleState {
#[default]
Inactive,
Active,
}
impl<T: std::borrow::Borrow<bool>> From<T> for ToggleState {
fn from(item: T) -> Self {
match *item.borrow() {
true => Self::Active,
false => Self::Inactive,
}
}
}
impl<T> Toggleable<T> { impl<T> Toggleable<T> {
pub fn new(active: T, inactive: T) -> Self { pub fn new(active: T, inactive: T) -> Self {
Self { active, inactive } Self { active, inactive }
} }
pub fn in_state(&self, state: impl Into<ToggleState>) -> &T { pub fn in_state(&self, active: bool) -> &T {
match state.into() { if active {
ToggleState::Inactive => &self.inactive, &self.inactive
ToggleState::Active => &self.active, } else {
&self.active
} }
} }
pub fn active_state(&self) -> &T { pub fn active_state(&self) -> &T {
self.in_state(ToggleState::Active) self.in_state(true)
} }
pub fn inactive_state(&self) -> &T { pub fn inactive_state(&self) -> &T {
self.in_state(ToggleState::Inactive) self.in_state(false)
} }
} }

View file

@ -6,7 +6,7 @@ use gpui::{
}; };
use serde::Deserialize; use serde::Deserialize;
use std::rc::Rc; use std::rc::Rc;
use theme::{ThemeSettings, ToggleState}; use theme::ThemeSettings;
pub trait Panel: View { pub trait Panel: View {
fn position(&self, cx: &WindowContext) -> DockPosition; fn position(&self, cx: &WindowContext) -> DockPosition;
@ -498,12 +498,7 @@ impl View for PanelButtons {
Stack::new() Stack::new()
.with_child( .with_child(
MouseEventHandler::<Self, _>::new(panel_ix, cx, |state, cx| { MouseEventHandler::<Self, _>::new(panel_ix, cx, |state, cx| {
let toggle_state = if is_active { let style = button_style.in_state(is_active);
ToggleState::Active
} else {
ToggleState::Inactive
};
let style = button_style.in_state(toggle_state);
let style = style.style_for(state); let style = style.style_for(state);
Flex::row() Flex::row()