Skip to content

Commit

Permalink
add way to check if overlay was clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
n00kii committed Oct 15, 2022
1 parent c60b399 commit a4034e0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui-modal"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
license = "MIT"
description = "a modal library for egui"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# egui-modal, a modal library for [`egui`](https://github.com/emilk/egui)
[![crates.io](https://img.shields.io/crates/v/egui-modal)](https://crates.io/crates/egui-modal/0.1.5)
[![docs](https://docs.rs/egui-modal/badge.svg)](https://docs.rs/egui-modal/0.1.5/egui_modal/)
[![crates.io](https://img.shields.io/crates/v/egui-modal)](https://crates.io/crates/egui-modal/0.1.6)
[![docs](https://docs.rs/egui-modal/badge.svg)](https://docs.rs/egui-modal/0.1.6/egui_modal/)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/n00kii/egui-modal/blob/main/README.md)

![modal](https://raw.githubusercontent.com/n00kii/egui-modal/main/media/modal.png?token=GHSAT0AAAAAABVWXBGJBQSFC3PLQP4KKOG6YZJIDCA)
Expand Down
7 changes: 4 additions & 3 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl eframe::App for ExampleApp {
if self.include_title {
modal.title(ui, &mut self.modal_title);
}
// the "frame" of the modal refers to the container of the icon and body.
// the "frame" of the modal refers to the container of the icon and body.
// this helper just applies a margin specified by the ModalStyle
modal.frame(ui, |ui| {
if self.include_body {
Expand All @@ -59,7 +59,9 @@ impl eframe::App for ExampleApp {
});
if self.include_buttons {
modal.buttons(ui, |ui| {
if modal.button(ui, "close").clicked() {
if modal.button(ui, "close").clicked()
|| (self.close_on_outside_click && modal.was_outside_clicked())
{
// all buttons created with the helper functions automatically
// close the modal on click, but you can close it yourself with
// ['modal.close()']
Expand Down Expand Up @@ -148,7 +150,6 @@ impl eframe::App for ExampleApp {
ui.add_sized(ui.available_rect_before_wrap().size(), icon_size);
ui.end_row();


ui.label("dialog icon");
let mut use_icon = self.dialog_icon.is_some();
ui.horizontal(|ui| {
Expand Down
30 changes: 24 additions & 6 deletions src/modal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use egui::{
emath::{Align, Align2},
epaint::{Color32, Pos2, Rounding},
Area, Button, Context, Id, Layout, Response, RichText, Sense, Ui, Window, WidgetText,
Area, Button, Context, Id, Layout, Response, RichText, Sense, Ui, WidgetText, Window,
};

const ERROR_ICON_COLOR: Color32 = Color32::from_rgb(200, 90, 90);
Expand All @@ -26,7 +26,7 @@ pub enum ModalButtonStyle {
Caution,
}

/// An icon. If used, it will be shown next to the body of
/// An icon. If used, it will be shown next to the body of
/// the modal.
#[derive(Clone, Default, PartialEq)]
pub enum Icon {
Expand Down Expand Up @@ -74,6 +74,7 @@ enum ModalType {
/// Information about the current state of the modal. (Pretty empty
/// right now but may be expanded upon in the future.)
struct ModalState {
was_outside_clicked: bool,
is_open: bool,
modal_type: ModalType,
}
Expand Down Expand Up @@ -116,7 +117,7 @@ pub struct ModalStyle {
/// The color of the success icon
pub success_icon_color: Color32,
/// The color of the error icon
pub error_icon_color: Color32,
pub error_icon_color: Color32,

/// The alignment of text inside the body
pub body_alignment: Align,
Expand All @@ -134,6 +135,7 @@ impl ModalState {
impl Default for ModalState {
fn default() -> Self {
Self {
was_outside_clicked: false,
is_open: false,
modal_type: ModalType::Modal,
}
Expand Down Expand Up @@ -226,6 +228,18 @@ impl Modal {
modal_state.save(&self.ctx, self.id)
}

fn set_outside_clicked(&self, was_clicked: bool) {
let mut modal_state = ModalState::load(&self.ctx, self.id);
modal_state.was_outside_clicked = was_clicked;
modal_state.save(&self.ctx, self.id)
}

/// Was the outer overlay clicked this frame?
pub fn was_outside_clicked(&self) -> bool {
let modal_state = ModalState::load(&self.ctx, self.id);
modal_state.was_outside_clicked
}

/// Open the modal; make it visible. The modal prevents user input to other parts of the
/// application.
pub fn open(&self) {
Expand Down Expand Up @@ -259,7 +273,7 @@ impl Modal {
/// });
/// ```
pub fn title(&self, ui: &mut Ui, text: impl Into<RichText>) {
let text: RichText = text.into();
let text: RichText = text.into();
ui.vertical_centered(|ui| {
ui.heading(text);
});
Expand Down Expand Up @@ -414,8 +428,11 @@ impl Modal {
.show(&self.ctx, |ui: &mut Ui| {
let screen_rect = ui.ctx().input().screen_rect;
let area_response = ui.allocate_response(screen_rect.size(), Sense::click());
if area_response.clicked() && self.close_on_outside_click {
self.close();
if area_response.clicked() {
self.set_outside_clicked(true);
if self.close_on_outside_click {
self.close();
}
}
ui.painter().rect_filled(
screen_rect,
Expand All @@ -436,6 +453,7 @@ impl Modal {
inner_response.response.request_focus();
ctx_clone.move_to_top(inner_response.response.layer_id);
}
self.set_outside_clicked(false);
}
}

Expand Down

0 comments on commit a4034e0

Please sign in to comment.