Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard shortcut helpers #2202

Merged
merged 17 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,9 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG

### Added ⭐
* Added helper functions for animating panels that collapse/expand ([#2190](https://github.com/emilk/egui/pull/2190)).
* Added `Context::os/Context::set_os` to query/set what operating system egui believes it is running on ([#2202](https://github.com/emilk/egui/pull/2202)).
* Added `Button::shortcut_text` for showing keyboard shortcuts in menu buttons ([#2202](https://github.com/emilk/egui/pull/2202)).
* Added `egui::KeyboardShortcut` for showing keyboard shortcuts in menu buttons ([#2202](https://github.com/emilk/egui/pull/2202)).

### Fixed 🐛
* ⚠️ BREAKING: Fix text being too small ([#2069](https://github.com/emilk/egui/pull/2069)).
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/CHANGELOG.md
Expand Up @@ -14,7 +14,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
* Added `center` to `NativeOptions` and `monitor_size` to `WindowInfo` on desktop ([#2035](https://github.com/emilk/egui/pull/2035)).
* Web: you can access your application from JS using `AppRunner::app_mut`. See `crates/egui_demo_app/src/lib.rs`.
* Web: You can now use WebGL on top of `wgpu` by enabling the `wgpu` feature (and disabling `glow` via disabling default features) ([#2107](https://github.com/emilk/egui/pull/2107)).

* Web: Add `WebInfo::user_agent` ([#2202](https://github.com/emilk/egui/pull/2202)).


## 0.19.0 - 2022-08-20
Expand Down
3 changes: 3 additions & 0 deletions crates/eframe/src/epi.rs
Expand Up @@ -767,6 +767,9 @@ impl Frame {
#[derive(Clone, Debug)]
#[cfg(target_arch = "wasm32")]
pub struct WebInfo {
/// The browser user agent.
pub user_agent: String,

/// Information about the URL.
pub location: Location,
}
Expand Down
8 changes: 8 additions & 0 deletions crates/eframe/src/web/backend.rs
Expand Up @@ -87,6 +87,10 @@ impl IsDestroyed {

// ----------------------------------------------------------------------------

fn user_agent() -> Option<String> {
web_sys::window()?.navigator().user_agent().ok()
}

fn web_location() -> epi::Location {
let location = web_sys::window().unwrap().location();

Expand Down Expand Up @@ -198,6 +202,7 @@ impl AppRunner {

let info = epi::IntegrationInfo {
web_info: epi::WebInfo {
user_agent: user_agent().unwrap_or_default(),
location: web_location(),
},
system_theme,
Expand All @@ -207,6 +212,9 @@ impl AppRunner {
let storage = LocalStorage::default();

let egui_ctx = egui::Context::default();
egui_ctx.set_os(egui::os::OperatingSystem::from_user_agent(
&user_agent().unwrap_or_default(),
));
load_memory(&egui_ctx);

let theme = system_theme.unwrap_or(web_options.default_theme);
Expand Down
58 changes: 57 additions & 1 deletion crates/egui/src/context.rs
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;

use crate::{
animation_manager::AnimationManager, data::output::PlatformOutput, frame_state::FrameState,
input_state::*, layers::GraphicLayers, memory::Options, output::FullOutput, TextureHandle, *,
input_state::*, layers::GraphicLayers, memory::Options, os::OperatingSystem,
output::FullOutput, TextureHandle, *,
};
use epaint::{mutex::*, stats::*, text::Fonts, textures::TextureFilter, TessellationOptions, *};

Expand Down Expand Up @@ -36,6 +37,8 @@ struct ContextImpl {
animation_manager: AnimationManager,
tex_manager: WrappedTextureManager,

os: OperatingSystem,

input: InputState,

/// State that is collected during a frame and then cleared
Expand Down Expand Up @@ -563,6 +566,59 @@ impl Context {
pub fn tessellation_options(&self) -> RwLockWriteGuard<'_, TessellationOptions> {
RwLockWriteGuard::map(self.write(), |c| &mut c.memory.options.tessellation_options)
}

/// What operating system are we running on?
///
/// When compiling natively, this is
/// figured out from the `target_os`.
///
/// For web, this can be figured out from the user-agent,
/// and is done so by [`eframe`](https://github.com/emilk/egui/tree/master/crates/eframe).
pub fn os(&self) -> OperatingSystem {
self.read().os
}

/// Set the operating system we are running on.
///
/// If you are writing wasm-based integration for egui you
/// may want to set this based on e.g. the user-agent.
pub fn set_os(&self, os: OperatingSystem) {
self.write().os = os;
}

/// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`).
///
/// Can be used to get the text for [`Button::shortcut_text`].
pub fn format_shortcut(&self, shortcut: &KeyboardShortcut) -> String {
let os = self.os();

let is_mac = matches!(os, OperatingSystem::Mac | OperatingSystem::IOS);

let can_show_symbols = || {
let ModifierNames {
alt,
ctrl,
shift,
mac_cmd,
..
} = ModifierNames::SYMBOLS;

let font_id = TextStyle::Body.resolve(&self.style());
let fonts = self.fonts();
let mut fonts = fonts.lock();
let font = fonts.fonts.font(&font_id);
font.has_glyphs(alt)
&& font.has_glyphs(ctrl)
&& font.has_glyphs(shift)
&& font.has_glyphs(mac_cmd)
};

if is_mac && can_show_symbols() {
shortcut.format(&ModifierNames::SYMBOLS, is_mac)
} else {
shortcut.format(&ModifierNames::NAMES, is_mac)
}
}
}

impl Context {
Expand Down