Skip to content

Commit

Permalink
[button] add ability to disable Button
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jun 22, 2020
1 parent 10a9d48 commit 67bafad
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl CollapsingHeader {
.text_style(TextStyle::Button)
.multiline(false),
default_open: false,
id_source : None,
id_source: None,
}
}

Expand Down
6 changes: 6 additions & 0 deletions egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ impl Context {
if interaction_id.is_none() || sense == Sense::nothing() {
// Not interested in input:
return InteractInfo {
sense,
rect,
hovered,
clicked: false,
Expand All @@ -341,6 +342,7 @@ impl Context {
if self.input.mouse.pressed {
if hovered {
let mut info = InteractInfo {
sense,
rect,
hovered: true,
clicked: false,
Expand Down Expand Up @@ -368,6 +370,7 @@ impl Context {
} else {
// miss
InteractInfo {
sense,
rect,
hovered,
clicked: false,
Expand All @@ -378,6 +381,7 @@ impl Context {
} else if self.input.mouse.released {
let clicked = hovered && active;
InteractInfo {
sense,
rect,
hovered,
clicked,
Expand All @@ -386,6 +390,7 @@ impl Context {
}
} else if self.input.mouse.down {
InteractInfo {
sense,
rect,
hovered: hovered && active,
clicked: false,
Expand All @@ -394,6 +399,7 @@ impl Context {
}
} else {
InteractInfo {
sense,
rect,
hovered,
clicked: false,
Expand Down
10 changes: 5 additions & 5 deletions egui/src/examples/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl ExampleWindow {
#[cfg_attr(feature = "with_serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "with_serde", serde(default))]
struct Widgets {
checked: bool,
button_enabled: bool,
count: usize,
radio: usize,
slider_value: f32,
Expand All @@ -285,7 +285,7 @@ struct Widgets {
impl Default for Widgets {
fn default() -> Self {
Self {
checked: true,
button_enabled: true,
radio: 0,
count: 0,
slider_value: 3.14,
Expand All @@ -305,8 +305,6 @@ impl Widgets {
);
});

ui.add(Checkbox::new(&mut self.checked, "checkbox"));

ui.horizontal(|ui| {
if ui.add(radio(self.radio == 0, "First")).clicked {
self.radio = 0;
Expand All @@ -319,9 +317,11 @@ impl Widgets {
}
});

ui.add(Checkbox::new(&mut self.button_enabled, "Button enabled"));

ui.inner_layout(Layout::horizontal(Align::Center), |ui| {
if ui
.add(Button::new("Click me"))
.add(Button::new("Click me").enabled(self.button_enabled))
.tooltip_text("This will just increase a counter.")
.clicked
{
Expand Down
13 changes: 12 additions & 1 deletion egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct Interact {
pub active: WidgetStyle,
pub hovered: WidgetStyle,
pub inactive: WidgetStyle,
pub disabled: WidgetStyle,
}

impl Default for Interact {
Expand Down Expand Up @@ -133,13 +134,23 @@ impl Default for Interact {
rect_outline: Some(LineStyle::new(1.0, white(128))),
corner_radius: 4.0,
},
disabled: WidgetStyle {
bg_fill: None,
fill: srgba(50, 50, 50, 255),
stroke_color: gray(128, 255), // Should look grayed out
stroke_width: 0.5,
rect_outline: Some(LineStyle::new(0.5, white(128))),
corner_radius: 4.0,
},
}
}
}

impl Interact {
pub fn style(&self, interact: &InteractInfo) -> &WidgetStyle {
if interact.active {
if interact.sense == Sense::nothing() {
&self.disabled
} else if interact.active {
&self.active
} else if interact.hovered {
&self.hovered
Expand Down
18 changes: 18 additions & 0 deletions egui/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ impl Default for CursorIcon {
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize))]
pub struct InteractInfo {
/// The senses (click or drag) that the widget is interested in (if any).
pub sense: Sense,

/// The mouse is hovering above this thing
pub hovered: bool,

Expand All @@ -59,6 +62,7 @@ pub struct InteractInfo {
impl InteractInfo {
pub fn nothing() -> Self {
Self {
sense: Sense::nothing(),
hovered: false,
clicked: false,
double_clicked: false,
Expand All @@ -69,6 +73,7 @@ impl InteractInfo {

pub fn union(self, other: Self) -> Self {
Self {
sense: self.sense.union(other.sense),
hovered: self.hovered || other.hovered,
clicked: self.clicked || other.clicked,
double_clicked: self.double_clicked || other.double_clicked,
Expand All @@ -82,6 +87,9 @@ impl InteractInfo {

// TODO: rename GuiResponse
pub struct GuiResponse {
/// The senses (click or drag) that the widget is interested in (if any).
pub sense: Sense,

/// The mouse is hovering above this
pub hovered: bool,

Expand Down Expand Up @@ -120,6 +128,7 @@ impl GuiResponse {
impl Into<InteractInfo> for GuiResponse {
fn into(self) -> InteractInfo {
InteractInfo {
sense: self.sense,
hovered: self.hovered,
clicked: self.clicked,
double_clicked: self.double_clicked,
Expand All @@ -133,6 +142,7 @@ impl Into<InteractInfo> for GuiResponse {

/// What sort of interaction is a widget sensitive to?
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize))]
pub struct Sense {
/// buttons, sliders, windows ...
pub click: bool,
Expand Down Expand Up @@ -170,4 +180,12 @@ impl Sense {
drag: true,
}
}

#[must_use]
pub fn union(self, other: Self) -> Self {
Self {
click: self.click | other.click,
drag: self.drag | other.drag,
}
}
}
25 changes: 17 additions & 8 deletions egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ impl Ui {
&mut self,
explicit_id_source: Option<Id>,
default_id_source: Option<&str>,
) -> Id
{
) -> Id {
let id = if let Some(explicit_id_source) = explicit_id_source {
self.id.with(&explicit_id_source)
} else {
Expand All @@ -315,7 +314,8 @@ impl Ui {
self.make_position_id()
}
};
self.ctx.register_unique_id(id, default_id_source.unwrap_or_default(), self.cursor)
self.ctx
.register_unique_id(id, default_id_source.unwrap_or_default(), self.cursor)
}

/// Make an Id that is unique to this positon.
Expand Down Expand Up @@ -349,12 +349,21 @@ impl Ui {
#[must_use]
pub fn response(&mut self, interact: InteractInfo) -> GuiResponse {
// TODO: unify GuiResponse and InteractInfo. They are the same thing!
let InteractInfo {
sense,
hovered,
clicked,
double_clicked,
active,
rect,
} = interact;
GuiResponse {
hovered: interact.hovered,
clicked: interact.clicked,
double_clicked: interact.double_clicked,
active: interact.active,
rect: interact.rect,
sense,
hovered,
clicked,
double_clicked,
active,
rect,
ctx: self.ctx.clone(),
}
}
Expand Down
9 changes: 9 additions & 0 deletions egui/src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ impl Button {
self.sense = sense;
self
}

/// If you set this to `false`, the button will be grayed out and un-clickable.
/// `enabled(false)` has the same effect as calling `sense(Sense::nothing())`.
pub fn enabled(mut self, enabled: bool) -> Self {
if !enabled {
self.sense = Sense::nothing();
}
self
}
}

impl Widget for Button {
Expand Down

0 comments on commit 67bafad

Please sign in to comment.