Keep the compositor's fullscreen, and copy/paste in every text field
Fullscreen: applying settings (any option button, joining a world, travelling, reloading packs) compared the Fullscreen option with what the window reported and forced the window back to the option. A window made fullscreen by the compositor (a Hyprland keybind or window rule) left fullscreen on the next button click. The window now follows blaze3d Window's fullscreenRequested/fullscreen pair: it is only asked to change when the option changes (updateFullscreenIfChanged), a change the window system makes is adopted by the option (updateFullscreenState -> Minecraft.fullscreenStateChanged), and the window is created fullscreen when options.txt says so. Clipboard: Ctrl+A/C/V/X (Cmd on macOS) in the chat, signs, book and quill (page and title), anvil, creative and recipe-book searches, the language and pack searches, the LAN port, world name and seed, and the server name and address boxes, through the arboard clipboard (Wayland data-control, falling back to X11). Shortcuts follow InputWithModifiers.isCopy/isPaste/ isCut/isSelectAll on the modifier state and the layout's letter; pasted text goes through each field's own vanilla rules: StringUtil.filterText and setMaxLength for EditBox, line breaks kept and the 1024-character and 14-line limits for MultiLineEditBox, formatting stripped and whole-or- nothing width checks for TextFieldHelper on signs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -432,6 +432,18 @@ impl ChatScreen {
|
||||
self.allow_suggestions = true;
|
||||
self.update_command_info(m, c);
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the input box.
|
||||
pub fn shortcut(
|
||||
&mut self,
|
||||
m: &Measure,
|
||||
c: &Commands,
|
||||
shortcut: crate::clipboard::EditShortcut,
|
||||
clipboard: &mut dyn crate::clipboard::Clipboard,
|
||||
) {
|
||||
if self.input.shortcut(m, shortcut, clipboard) {
|
||||
self.edited(m, c);
|
||||
}
|
||||
}
|
||||
/// Typed text (`charTyped` per character).
|
||||
pub fn type_text(&mut self, m: &Measure, c: &Commands, text: &str) {
|
||||
for ch in text.chars() {
|
||||
|
||||
@@ -0,0 +1,455 @@
|
||||
//! The system clipboard and the text-editing shortcuts of 26.3:
|
||||
//! `KeyboardHandler.getClipboard`/`setClipboard`, `InputWithModifiers.isSelectAll`,
|
||||
//! `isCopy`, `isPaste` and `isCut`, `StringUtil.filterText` and
|
||||
//! `ChatFormatting.stripFormatting`.
|
||||
|
||||
use winit::keyboard::{Key, KeyCode, PhysicalKey};
|
||||
|
||||
/// A text-editing shortcut (`InputWithModifiers.isSelectAll`, `isCopy`,
|
||||
/// `isPaste`, `isCut`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum EditShortcut {
|
||||
SelectAll,
|
||||
Copy,
|
||||
Paste,
|
||||
Cut,
|
||||
}
|
||||
|
||||
/// The letter a key stands for in shortcuts (`KeyEvent.shortcutKey`, SDL's
|
||||
/// keycode): the layout's letter, or the key's US-layout letter when the
|
||||
/// layout's is not a Latin one (SDL's default `latin_letters` keycode option).
|
||||
pub fn shortcut_letter(logical: &Key, physical: PhysicalKey) -> Option<char> {
|
||||
if let Key::Character(s) = logical {
|
||||
let mut chars = s.chars();
|
||||
if let (Some(c), None) = (chars.next(), chars.next())
|
||||
&& c.is_ascii_alphabetic()
|
||||
{
|
||||
return Some(c.to_ascii_lowercase());
|
||||
}
|
||||
}
|
||||
let PhysicalKey::Code(code) = physical else {
|
||||
return None;
|
||||
};
|
||||
Some(match code {
|
||||
KeyCode::KeyA => 'a',
|
||||
KeyCode::KeyC => 'c',
|
||||
KeyCode::KeyV => 'v',
|
||||
KeyCode::KeyX => 'x',
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// `InputWithModifiers.isSelectAll`/`isCopy`/`isPaste`/`isCut`: A, C, V or X
|
||||
/// with the edit modifier (`InputQuirks.EDIT_SHORTCUT_KEY_MODIFIER`: Cmd on
|
||||
/// macOS, Ctrl elsewhere) and neither Shift nor Alt.
|
||||
pub fn edit_shortcut(
|
||||
letter: Option<char>,
|
||||
edit_modifier: bool,
|
||||
shift: bool,
|
||||
alt: bool,
|
||||
) -> Option<EditShortcut> {
|
||||
if !edit_modifier || shift || alt {
|
||||
return None;
|
||||
}
|
||||
Some(match letter? {
|
||||
'a' => EditShortcut::SelectAll,
|
||||
'c' => EditShortcut::Copy,
|
||||
'v' => EditShortcut::Paste,
|
||||
'x' => EditShortcut::Cut,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// `StringUtil.isAllowedChatCharacter`.
|
||||
pub fn is_allowed_chat_character(c: char) -> bool {
|
||||
c != '§' && c >= ' ' && c != '\u{7f}'
|
||||
}
|
||||
|
||||
/// `StringUtil.filterText`: the allowed characters, and line breaks where
|
||||
/// the field takes several lines.
|
||||
pub fn filter_text(input: &str, multiline: bool) -> String {
|
||||
input
|
||||
.chars()
|
||||
.filter(|&c| is_allowed_chat_character(c) || (multiline && c == '\n'))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `ChatFormatting.stripFormatting`: `§` followed by a format code removed.
|
||||
pub fn strip_formatting(input: &str) -> String {
|
||||
let mut out = String::with_capacity(input.len());
|
||||
let mut chars = input.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '§'
|
||||
&& chars.peek().is_some_and(
|
||||
|n| matches!(n.to_ascii_lowercase(), '0'..='9' | 'a'..='f' | 'k'..='o' | 'r'),
|
||||
)
|
||||
{
|
||||
chars.next();
|
||||
continue;
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `TextFieldHelper.getClipboardContents`: formatting and carriage returns
|
||||
/// removed (signs).
|
||||
pub fn helper_clipboard_contents(clipboard: &str) -> String {
|
||||
strip_formatting(&clipboard.replace('\r', ""))
|
||||
}
|
||||
|
||||
/// A clipboard: the system's, or an in-memory one in tests.
|
||||
pub trait Clipboard {
|
||||
/// `KeyboardHandler.getClipboard`: empty when there is no text.
|
||||
fn get(&mut self) -> String;
|
||||
/// `KeyboardHandler.setClipboard`: empty text is not copied.
|
||||
fn set(&mut self, text: &str);
|
||||
}
|
||||
|
||||
/// The system clipboard (`ClipboardManager`). On Wayland, arboard uses the
|
||||
/// data-control protocol and falls back to X11 (XWayland) on compositors
|
||||
/// without it. When no system clipboard can be reached, text copied in the
|
||||
/// game can still be pasted in the game.
|
||||
#[derive(Default)]
|
||||
pub struct SystemClipboard {
|
||||
system: Option<arboard::Clipboard>,
|
||||
/// The text Horizon last copied (`horizon-ctl status`).
|
||||
copied: Option<String>,
|
||||
}
|
||||
|
||||
impl SystemClipboard {
|
||||
/// The text last copied by the game.
|
||||
pub fn copied(&self) -> Option<&str> {
|
||||
self.copied.as_deref()
|
||||
}
|
||||
fn system(&mut self) -> Option<&mut arboard::Clipboard> {
|
||||
if self.system.is_none() {
|
||||
self.system = arboard::Clipboard::new().ok();
|
||||
}
|
||||
self.system.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl Clipboard for SystemClipboard {
|
||||
fn get(&mut self) -> String {
|
||||
let Some(system) = self.system() else {
|
||||
return self.copied.clone().unwrap_or_default();
|
||||
};
|
||||
match system.get_text() {
|
||||
Ok(text) => text,
|
||||
Err(arboard::Error::ContentNotAvailable) => String::new(),
|
||||
Err(_) => {
|
||||
// A lost connection (a restarted compositor): reconnect next time.
|
||||
self.system = None;
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
fn set(&mut self, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
self.copied = Some(text.to_owned());
|
||||
if let Some(system) = self.system()
|
||||
&& system.set_text(text.to_owned()).is_err()
|
||||
{
|
||||
self.system = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A clipboard in memory.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct MemoryClipboard(pub String);
|
||||
|
||||
impl Clipboard for MemoryClipboard {
|
||||
fn get(&mut self) -> String {
|
||||
self.0.clone()
|
||||
}
|
||||
fn set(&mut self, text: &str) {
|
||||
if !text.is_empty() {
|
||||
self.0 = text.to_owned();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A text field with a cursor and a selection (`EditBox`).
|
||||
pub trait Editable {
|
||||
/// Select All: the cursor at the end, the selection from the start.
|
||||
fn select_all(&mut self);
|
||||
/// `getHighlighted`.
|
||||
fn selected_text(&self) -> String;
|
||||
/// `insertText`, with the field's own filter and length limit. True when
|
||||
/// the value changed.
|
||||
fn insert(&mut self, text: &str) -> bool;
|
||||
}
|
||||
|
||||
/// `EditBox.keyPressed`'s shortcuts. True when the value changed.
|
||||
pub fn apply(
|
||||
field: &mut dyn Editable,
|
||||
shortcut: EditShortcut,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
) -> bool {
|
||||
match shortcut {
|
||||
EditShortcut::SelectAll => {
|
||||
field.select_all();
|
||||
false
|
||||
}
|
||||
EditShortcut::Copy => {
|
||||
clipboard.set(&field.selected_text());
|
||||
false
|
||||
}
|
||||
EditShortcut::Paste => field.insert(&clipboard.get()),
|
||||
EditShortcut::Cut => {
|
||||
clipboard.set(&field.selected_text());
|
||||
field.insert("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Which append-only field a Select All covers: a name and an index (a
|
||||
/// sign line, a book page).
|
||||
pub type FieldId = (&'static str, usize);
|
||||
|
||||
/// The selection of Horizon's append-only fields, whose cursor stays at the
|
||||
/// end: Select All selects the whole value, which the next copy, cut, paste,
|
||||
/// typed text or backspace replaces. It is forgotten as soon as the field or
|
||||
/// its value changes.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct AppendSelection(Option<(FieldId, String)>);
|
||||
|
||||
impl AppendSelection {
|
||||
/// Whether `value` of field `id` is wholly selected.
|
||||
pub fn covers(&self, id: FieldId, value: &str) -> bool {
|
||||
self.0.as_ref().is_some_and(|(i, v)| *i == id && v == value)
|
||||
}
|
||||
/// Before typing or a backspace: an all-selected value is cleared (true).
|
||||
pub fn take(&mut self, id: FieldId, value: &mut String) -> bool {
|
||||
let covered = self.covers(id, value);
|
||||
self.0 = None;
|
||||
if covered {
|
||||
value.clear();
|
||||
}
|
||||
covered
|
||||
}
|
||||
/// A shortcut in an append-only field. `insert` adds pasted text to a
|
||||
/// value the field's own way and says whether it accepted it (a field
|
||||
/// that rejects it keeps its value, as `TextFieldHelper.insertText`).
|
||||
/// True when the value changed.
|
||||
pub fn apply(
|
||||
&mut self,
|
||||
id: FieldId,
|
||||
value: &mut String,
|
||||
shortcut: EditShortcut,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
insert: impl FnOnce(&mut String, &str) -> bool,
|
||||
) -> bool {
|
||||
let covered = self.covers(id, value);
|
||||
match shortcut {
|
||||
EditShortcut::SelectAll => {
|
||||
self.0 = Some((id, value.clone()));
|
||||
false
|
||||
}
|
||||
EditShortcut::Copy => {
|
||||
if covered {
|
||||
clipboard.set(value);
|
||||
}
|
||||
false
|
||||
}
|
||||
EditShortcut::Cut => {
|
||||
self.0 = None;
|
||||
if !covered {
|
||||
return false;
|
||||
}
|
||||
clipboard.set(value);
|
||||
let changed = !value.is_empty();
|
||||
value.clear();
|
||||
changed
|
||||
}
|
||||
EditShortcut::Paste => {
|
||||
self.0 = None;
|
||||
let text = clipboard.get();
|
||||
let mut next = if covered {
|
||||
String::new()
|
||||
} else {
|
||||
value.clone()
|
||||
};
|
||||
if !insert(&mut next, &text) || next == *value {
|
||||
return false;
|
||||
}
|
||||
*value = next;
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `EditBox.insertText` for a field without a cursor: the filtered text,
|
||||
/// cut to the room left by `max_chars`.
|
||||
pub fn append_limited(value: &mut String, text: &str, max_chars: usize) -> bool {
|
||||
let room = max_chars.saturating_sub(value.chars().count());
|
||||
if room == 0 {
|
||||
return false;
|
||||
}
|
||||
value.extend(filter_text(text, false).chars().take(room));
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn shortcuts_need_the_edit_modifier_alone() {
|
||||
let c = Some('c');
|
||||
assert_eq!(
|
||||
edit_shortcut(c, true, false, false),
|
||||
Some(EditShortcut::Copy)
|
||||
);
|
||||
assert_eq!(
|
||||
edit_shortcut(Some('v'), true, false, false),
|
||||
Some(EditShortcut::Paste)
|
||||
);
|
||||
assert_eq!(
|
||||
edit_shortcut(Some('x'), true, false, false),
|
||||
Some(EditShortcut::Cut)
|
||||
);
|
||||
assert_eq!(
|
||||
edit_shortcut(Some('a'), true, false, false),
|
||||
Some(EditShortcut::SelectAll)
|
||||
);
|
||||
assert_eq!(edit_shortcut(c, false, false, false), None);
|
||||
assert_eq!(edit_shortcut(c, true, true, false), None);
|
||||
assert_eq!(edit_shortcut(c, true, false, true), None);
|
||||
assert_eq!(edit_shortcut(Some('b'), true, false, false), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_layout_letter_decides_and_non_latin_layouts_use_the_position() {
|
||||
let q = PhysicalKey::Code(KeyCode::KeyQ);
|
||||
// AZERTY: the key at Q's position types an A.
|
||||
assert_eq!(shortcut_letter(&Key::Character("a".into()), q), Some('a'));
|
||||
// Russian: the key at C's position types с.
|
||||
let c = PhysicalKey::Code(KeyCode::KeyC);
|
||||
assert_eq!(shortcut_letter(&Key::Character("с".into()), c), Some('c'));
|
||||
assert_eq!(shortcut_letter(&Key::Character("C".into()), q), Some('c'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_text_keeps_line_breaks_only_for_multiline_fields() {
|
||||
let s = "a\u{7}b§c\nd\re\u{80}";
|
||||
assert_eq!(filter_text(s, false), "abcde\u{80}");
|
||||
assert_eq!(filter_text(s, true), "abc\nde\u{80}");
|
||||
assert_eq!(strip_formatting("§aRed§r §zx§"), "Red §zx§");
|
||||
assert_eq!(helper_clipboard_contents("§lone\r\ntwo"), "one\ntwo");
|
||||
}
|
||||
|
||||
struct Field {
|
||||
value: String,
|
||||
cursor: usize,
|
||||
highlight: usize,
|
||||
}
|
||||
impl Editable for Field {
|
||||
fn select_all(&mut self) {
|
||||
self.cursor = self.value.len();
|
||||
self.highlight = 0;
|
||||
}
|
||||
fn selected_text(&self) -> String {
|
||||
let (a, b) = (
|
||||
self.cursor.min(self.highlight),
|
||||
self.cursor.max(self.highlight),
|
||||
);
|
||||
self.value[a..b].to_owned()
|
||||
}
|
||||
fn insert(&mut self, text: &str) -> bool {
|
||||
let (a, b) = (
|
||||
self.cursor.min(self.highlight),
|
||||
self.cursor.max(self.highlight),
|
||||
);
|
||||
self.value.replace_range(a..b, text);
|
||||
self.cursor = a + text.len();
|
||||
self.highlight = self.cursor;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_box_shortcuts() {
|
||||
let mut clip = MemoryClipboard("old".into());
|
||||
let mut f = Field {
|
||||
value: "hello".into(),
|
||||
cursor: 5,
|
||||
highlight: 5,
|
||||
};
|
||||
// Nothing selected: copying leaves the clipboard alone (setClipboard skips "").
|
||||
apply(&mut f, EditShortcut::Copy, &mut clip);
|
||||
assert_eq!(clip.0, "old");
|
||||
apply(&mut f, EditShortcut::SelectAll, &mut clip);
|
||||
apply(&mut f, EditShortcut::Copy, &mut clip);
|
||||
assert_eq!(clip.0, "hello");
|
||||
assert!(apply(&mut f, EditShortcut::Cut, &mut clip));
|
||||
assert_eq!(f.value, "");
|
||||
assert!(apply(&mut f, EditShortcut::Paste, &mut clip));
|
||||
assert!(apply(&mut f, EditShortcut::Paste, &mut clip));
|
||||
assert_eq!(f.value, "hellohello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_only_fields_select_everything_or_nothing() {
|
||||
let id = ("search", 0);
|
||||
let mut clip = MemoryClipboard("stone".into());
|
||||
let mut sel = AppendSelection::default();
|
||||
let mut value = String::from("dirt");
|
||||
let insert = |v: &mut String, t: &str| append_limited(v, t, 6);
|
||||
// Without Select All, copy and cut do nothing and paste appends.
|
||||
sel.apply(id, &mut value, EditShortcut::Copy, &mut clip, insert);
|
||||
assert_eq!(clip.0, "stone");
|
||||
assert!(!sel.apply(id, &mut value, EditShortcut::Cut, &mut clip, insert));
|
||||
assert!(sel.apply(id, &mut value, EditShortcut::Paste, &mut clip, insert));
|
||||
assert_eq!(value, "dirtst");
|
||||
// Select All then paste replaces the value.
|
||||
sel.apply(id, &mut value, EditShortcut::SelectAll, &mut clip, insert);
|
||||
assert!(sel.apply(id, &mut value, EditShortcut::Paste, &mut clip, insert));
|
||||
assert_eq!(value, "stone");
|
||||
// Select All then cut empties it and copies it.
|
||||
sel.apply(id, &mut value, EditShortcut::SelectAll, &mut clip, insert);
|
||||
clip.0 = "x".into();
|
||||
assert!(sel.apply(id, &mut value, EditShortcut::Cut, &mut clip, insert));
|
||||
assert_eq!((value.as_str(), clip.0.as_str()), ("", "stone"));
|
||||
// A selection is forgotten once the value changes or another field is used.
|
||||
value = "ab".into();
|
||||
sel.apply(id, &mut value, EditShortcut::SelectAll, &mut clip, insert);
|
||||
assert!(!sel.covers(("other", 0), &value));
|
||||
value.push('c');
|
||||
assert!(!sel.take(id, &mut value));
|
||||
assert_eq!(value, "abc");
|
||||
sel.apply(id, &mut value, EditShortcut::SelectAll, &mut clip, insert);
|
||||
assert!(sel.take(id, &mut value));
|
||||
assert_eq!(value, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rejected_paste_keeps_the_selected_value() {
|
||||
// TextFieldHelper.insertText: a line too wide is not set at all.
|
||||
let id = ("sign", 1);
|
||||
let mut clip = MemoryClipboard("far too long".into());
|
||||
let mut sel = AppendSelection::default();
|
||||
let mut value = String::from("ok");
|
||||
sel.apply(
|
||||
id,
|
||||
&mut value,
|
||||
EditShortcut::SelectAll,
|
||||
&mut clip,
|
||||
|_, _| true,
|
||||
);
|
||||
let fits = |v: &mut String, t: &str| {
|
||||
let next = format!("{v}{t}");
|
||||
next.len() <= 5 && {
|
||||
*v = next;
|
||||
true
|
||||
}
|
||||
};
|
||||
assert!(!sel.apply(id, &mut value, EditShortcut::Paste, &mut clip, fits));
|
||||
assert_eq!(value, "ok");
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ mod bossbars;
|
||||
mod breezes;
|
||||
mod cartography;
|
||||
mod chat;
|
||||
mod clipboard;
|
||||
mod combat;
|
||||
mod command_harness;
|
||||
mod commands;
|
||||
@@ -626,6 +627,10 @@ struct Game<'a> {
|
||||
cursor: (f32, f32),
|
||||
captured: bool,
|
||||
focused: bool,
|
||||
/// The keyboard's modifier state (`KeyEvent.modifiers`).
|
||||
modifiers: winit::keyboard::ModifiersState,
|
||||
/// The window's fullscreen request and reported state.
|
||||
fullscreen: horizon_client::window::FullscreenState,
|
||||
/// MouseHandler's accumulated look and wheel state.
|
||||
mouse: MouseLook,
|
||||
/// `Options.smoothCamera` (cinematic camera), not persisted.
|
||||
@@ -841,9 +846,10 @@ struct Game<'a> {
|
||||
pause_menu: bool,
|
||||
/// MultiPlayerGameMode.previousLocalPlayerMode (F3+N goes back to it).
|
||||
previous_mode: Option<horizon_client::command::args::GameMode>,
|
||||
clipboard: Option<arboard::Clipboard>,
|
||||
/// The text last put on the clipboard (reported by `horizon-ctl status`).
|
||||
copied: Option<String>,
|
||||
/// The system clipboard (`KeyboardHandler.clipboardManager`).
|
||||
clipboard: horizon_client::clipboard::SystemClipboard,
|
||||
/// Select All in the append-only text fields.
|
||||
text_selection: horizon_client::clipboard::AppendSelection,
|
||||
/// The world was saved for the application ending (`save_for_exit`).
|
||||
saved_for_exit: bool,
|
||||
/// The block a guest is acting on (game/lan_actions.rs).
|
||||
@@ -8046,10 +8052,14 @@ impl ApplicationHandler for Game<'_> {
|
||||
if self.window.is_some() {
|
||||
return;
|
||||
}
|
||||
// The Window constructor: created fullscreen when options.txt says so.
|
||||
let fullscreen = self.settings.fullscreen;
|
||||
self.fullscreen = horizon_client::window::FullscreenState::new(fullscreen);
|
||||
let window = match event_loop.create_window(
|
||||
Window::default_attributes()
|
||||
.with_title("Horizon")
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(1280.0, 720.0)),
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(1280.0, 720.0))
|
||||
.with_fullscreen(fullscreen.then_some(winit::window::Fullscreen::Borderless(None))),
|
||||
) {
|
||||
Ok(w) => Arc::new(w),
|
||||
Err(e) => {
|
||||
@@ -8085,6 +8095,9 @@ impl ApplicationHandler for Game<'_> {
|
||||
if let WindowEvent::Focused(false) = &event {
|
||||
self.physical_keys.clear();
|
||||
}
|
||||
if let WindowEvent::ModifiersChanged(m) = &event {
|
||||
self.modifiers = m.state();
|
||||
}
|
||||
// KeyboardHandler.keyPress: the debug keys come before the game's.
|
||||
if let WindowEvent::KeyboardInput {
|
||||
event: key_event, ..
|
||||
@@ -8130,6 +8143,7 @@ impl ApplicationHandler for Game<'_> {
|
||||
if let Some(r) = &mut self.renderer {
|
||||
r.resize(size.width, size.height);
|
||||
}
|
||||
self.observe_fullscreen();
|
||||
}
|
||||
WindowEvent::Focused(focused) => {
|
||||
self.focused = focused;
|
||||
@@ -8367,19 +8381,12 @@ impl ApplicationHandler for Game<'_> {
|
||||
&& event.state == ElementState::Pressed
|
||||
&& !matches!(event.physical_key, PhysicalKey::Code(KeyCode::Escape)) =>
|
||||
{
|
||||
match event.physical_key {
|
||||
PhysicalKey::Code(KeyCode::Backspace) => {
|
||||
self.creative_screen.search.pop();
|
||||
}
|
||||
_ => {
|
||||
if let Some(text) = &event.text {
|
||||
self.creative_screen
|
||||
.search
|
||||
.extend(text.chars().filter(|c| !c.is_control()));
|
||||
}
|
||||
}
|
||||
if let Some(shortcut) = self.edit_shortcut(&event) {
|
||||
self.creative_search_shortcut(shortcut);
|
||||
} else {
|
||||
let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace);
|
||||
self.creative_search_edit(event.text.as_deref(), backspace);
|
||||
}
|
||||
self.creative_screen.scroll = 0.0;
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. }
|
||||
if self.recipe_search_focused()
|
||||
@@ -8395,8 +8402,12 @@ impl ApplicationHandler for Game<'_> {
|
||||
&& !matches!(event.physical_key, PhysicalKey::Code(KeyCode::Escape)) =>
|
||||
{
|
||||
// The anvil rename box takes every key but Escape (AnvilScreen.keyPressed).
|
||||
let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace);
|
||||
self.station_text(event.text.as_deref(), backspace);
|
||||
if let Some(shortcut) = self.edit_shortcut(&event) {
|
||||
self.station_shortcut(shortcut);
|
||||
} else {
|
||||
let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace);
|
||||
self.station_text(event.text.as_deref(), backspace);
|
||||
}
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } if self.remote_chat_open() => {
|
||||
self.remote_chat_key(&event)
|
||||
@@ -8405,6 +8416,11 @@ impl ApplicationHandler for Game<'_> {
|
||||
self.world_options_key(&event)
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } if self.screen == Screen::Options => {
|
||||
if let Some(shortcut) = self.edit_shortcut(&event)
|
||||
&& self.options_shortcut(shortcut)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if event.state == ElementState::Pressed
|
||||
&& let PhysicalKey::Code(key) = event.physical_key
|
||||
{
|
||||
@@ -8413,18 +8429,25 @@ impl ApplicationHandler for Game<'_> {
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } if self.title.is_some() => {
|
||||
if event.state == ElementState::Pressed
|
||||
&& let Some(title) = self.title.as_mut()
|
||||
&& !title.skip_opening()
|
||||
&& self.title.as_ref().is_some_and(|t| !t.skip_opening())
|
||||
{
|
||||
if let Some(shortcut) = self.edit_shortcut(&event) {
|
||||
self.title_shortcut(shortcut);
|
||||
return Ok(());
|
||||
}
|
||||
match event.physical_key {
|
||||
PhysicalKey::Code(KeyCode::Backspace) => title.backspace(),
|
||||
PhysicalKey::Code(KeyCode::Tab) => title.tab(),
|
||||
PhysicalKey::Code(KeyCode::Escape) => title.escape(),
|
||||
_ => {
|
||||
if let Some(text) = &event.text {
|
||||
title.type_text(text);
|
||||
PhysicalKey::Code(KeyCode::Backspace) => self.title_edit(None, true),
|
||||
PhysicalKey::Code(KeyCode::Tab) => {
|
||||
if let Some(t) = self.title.as_mut() {
|
||||
t.tab();
|
||||
}
|
||||
}
|
||||
PhysicalKey::Code(KeyCode::Escape) => {
|
||||
if let Some(t) = self.title.as_mut() {
|
||||
t.escape();
|
||||
}
|
||||
}
|
||||
_ => self.title_edit(event.text.as_deref(), false),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8484,6 +8507,7 @@ impl ApplicationHandler for Game<'_> {
|
||||
self.key_pressed(key)?;
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
self.observe_fullscreen();
|
||||
let now = Instant::now();
|
||||
let elapsed = now.duration_since(self.previous);
|
||||
self.frame_ticks = elapsed.as_secs_f32() * 20.0;
|
||||
@@ -8870,6 +8894,8 @@ pub fn run() -> Result<()> {
|
||||
cursor: (0.0, 0.0),
|
||||
captured: false,
|
||||
focused: true,
|
||||
modifiers: winit::keyboard::ModifiersState::empty(),
|
||||
fullscreen: horizon_client::window::FullscreenState::new(false),
|
||||
mouse: MouseLook::default(),
|
||||
smooth_camera: false,
|
||||
toggles: Default::default(),
|
||||
@@ -9027,8 +9053,8 @@ pub fn run() -> Result<()> {
|
||||
show_chunk_borders: false,
|
||||
pause_menu: true,
|
||||
previous_mode: None,
|
||||
clipboard: None,
|
||||
copied: None,
|
||||
clipboard: Default::default(),
|
||||
text_selection: Default::default(),
|
||||
saved_for_exit: false,
|
||||
forced_hit: None,
|
||||
lan_events: Vec::new(),
|
||||
|
||||
@@ -24,6 +24,8 @@ const BLACK: u32 = 0xFF00_0000;
|
||||
const PAGE_CHARS: usize = 1024;
|
||||
const MAX_PAGES: usize = 100;
|
||||
const LINES: usize = 14;
|
||||
/// `BookSignScreen`'s title box.
|
||||
const BOOK_TITLE: horizon_client::clipboard::FieldId = ("book.title", 0);
|
||||
|
||||
pub(super) enum BookScreen {
|
||||
Edit {
|
||||
@@ -128,6 +130,10 @@ impl Game<'_> {
|
||||
if event.state != ElementState::Pressed {
|
||||
return;
|
||||
}
|
||||
if let Some(shortcut) = self.edit_shortcut(event) {
|
||||
self.book_shortcut(shortcut);
|
||||
return;
|
||||
}
|
||||
let key = match event.physical_key {
|
||||
PhysicalKey::Code(k) => Some(k),
|
||||
_ => None,
|
||||
@@ -150,7 +156,9 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
Some(KeyCode::Backspace) => {
|
||||
title.pop();
|
||||
if !self.text_selection.take(BOOK_TITLE, title) {
|
||||
title.pop();
|
||||
}
|
||||
}
|
||||
Some(KeyCode::Enter | KeyCode::NumpadEnter) => {
|
||||
if !title.trim().is_empty() {
|
||||
@@ -159,6 +167,7 @@ impl Game<'_> {
|
||||
}
|
||||
_ => {
|
||||
if let Some(typed) = event.text.as_ref() {
|
||||
self.text_selection.take(BOOK_TITLE, title);
|
||||
for c in typed.chars().filter(|c| !c.is_control()) {
|
||||
if title.chars().count() < 15 {
|
||||
title.push(c);
|
||||
@@ -171,13 +180,23 @@ impl Game<'_> {
|
||||
Some(KeyCode::Escape) => self.save_book(false),
|
||||
Some(KeyCode::PageUp) => self.turn_page(false),
|
||||
Some(KeyCode::PageDown) => self.turn_page(true),
|
||||
Some(KeyCode::Backspace) => self.edit_page(|p| {
|
||||
p.pop();
|
||||
}),
|
||||
Some(KeyCode::Enter | KeyCode::NumpadEnter) => self.type_into_page("\n"),
|
||||
Some(KeyCode::Backspace) => {
|
||||
if !self.take_page_selection() {
|
||||
self.edit_page(|p| {
|
||||
p.pop();
|
||||
})
|
||||
}
|
||||
}
|
||||
Some(KeyCode::Enter | KeyCode::NumpadEnter) => {
|
||||
self.take_page_selection();
|
||||
self.type_into_page("\n")
|
||||
}
|
||||
_ => {
|
||||
if let Some(typed) = event.text.as_ref().map(|t| t.to_string()) {
|
||||
let typed: String = typed.chars().filter(|c| !c.is_control()).collect();
|
||||
if !typed.is_empty() {
|
||||
self.take_page_selection();
|
||||
}
|
||||
self.type_into_page(&typed);
|
||||
}
|
||||
}
|
||||
@@ -204,6 +223,59 @@ impl Game<'_> {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
/// Before typing on a page: Select All's selection is cleared (true).
|
||||
fn take_page_selection(&mut self) -> bool {
|
||||
match self.book.as_mut() {
|
||||
Some(BookScreen::Edit { pages, page, .. }) => self
|
||||
.text_selection
|
||||
.take(("book.page", *page), &mut pages[*page]),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
/// Ctrl+A/C/V/X: the title box (an `EditBox` of 15) while signing, else
|
||||
/// the page (`MultilineTextField.insertText`: line breaks kept, cut to
|
||||
/// the 1024 characters left, rejected when it overflows the 14 lines).
|
||||
fn book_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
|
||||
let font = &self.res.atlas.font;
|
||||
match self.book.as_mut() {
|
||||
Some(BookScreen::Edit {
|
||||
signing: true,
|
||||
title,
|
||||
..
|
||||
}) => {
|
||||
Self::append_shortcut(
|
||||
&mut self.text_selection,
|
||||
&mut self.clipboard,
|
||||
BOOK_TITLE,
|
||||
title,
|
||||
shortcut,
|
||||
15,
|
||||
);
|
||||
}
|
||||
Some(BookScreen::Edit { pages, page, .. }) => {
|
||||
// truncateInsertionText counts the whole value, selection included.
|
||||
let room = PAGE_CHARS.saturating_sub(pages[*page].chars().count());
|
||||
self.text_selection.apply(
|
||||
("book.page", *page),
|
||||
&mut pages[*page],
|
||||
shortcut,
|
||||
&mut self.clipboard,
|
||||
|value, pasted| {
|
||||
let text: String = horizon_client::clipboard::filter_text(pasted, true)
|
||||
.chars()
|
||||
.take(room)
|
||||
.collect();
|
||||
let next = format!("{value}{text}");
|
||||
font.split(&Text::literal(&next), 114.0).len() <= LINES && {
|
||||
*value = next;
|
||||
true
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn edit_page(&mut self, f: impl FnOnce(&mut String)) {
|
||||
if let Some(BookScreen::Edit { pages, page, .. }) = self.book.as_mut() {
|
||||
f(&mut pages[*page]);
|
||||
|
||||
@@ -418,6 +418,17 @@ impl Game<'_> {
|
||||
}
|
||||
/// Keys while the chat screen is open.
|
||||
pub(super) fn chat_key(&mut self, event: &KeyEvent) {
|
||||
if let Some(shortcut) = self.edit_shortcut(event) {
|
||||
let m = Measure(&self.res.atlas.font);
|
||||
let c = Commands {
|
||||
dispatcher: vanilla::game_dispatcher(),
|
||||
registries: &self.ui.registries,
|
||||
};
|
||||
if let Some(screen) = self.ui.chat_screen.as_mut() {
|
||||
screen.shortcut(&m, &c, shortcut, &mut self.clipboard);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let PhysicalKey::Code(key) = event.physical_key else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//! Copy and paste in the text fields: `KeyboardHandler`'s clipboard and the
|
||||
//! shortcuts of `EditBox`, `MultiLineEditBox` and `TextFieldHelper`.
|
||||
use super::*;
|
||||
use horizon_client::clipboard::{self, EditShortcut};
|
||||
use winit::event::KeyEvent;
|
||||
|
||||
impl Game<'_> {
|
||||
/// `InputWithModifiers.isSelectAll`/`isCopy`/`isPaste`/`isCut` of a key
|
||||
/// press (or repeat): the keyboard's modifier state, or a modifier key an
|
||||
/// agent holds.
|
||||
pub(super) fn edit_shortcut(&self, event: &KeyEvent) -> Option<EditShortcut> {
|
||||
if event.state != ElementState::Pressed {
|
||||
return None;
|
||||
}
|
||||
let m = self.modifiers;
|
||||
let held = |keys: [KeyCode; 2]| keys.iter().any(|k| self.keys.contains(k));
|
||||
// InputQuirks.EDIT_SHORTCUT_KEY_MODIFIER: Cmd on macOS, Ctrl elsewhere.
|
||||
let edit = if cfg!(target_os = "macos") {
|
||||
m.super_key() || held([KeyCode::SuperLeft, KeyCode::SuperRight])
|
||||
} else {
|
||||
m.control_key() || held([KeyCode::ControlLeft, KeyCode::ControlRight])
|
||||
};
|
||||
clipboard::edit_shortcut(
|
||||
clipboard::shortcut_letter(&event.logical_key, event.physical_key),
|
||||
edit,
|
||||
m.shift_key() || held([KeyCode::ShiftLeft, KeyCode::ShiftRight]),
|
||||
m.alt_key() || held([KeyCode::AltLeft, KeyCode::AltRight]),
|
||||
)
|
||||
}
|
||||
/// A shortcut in an append-only field limited to `max_chars` characters
|
||||
/// (an `EditBox` with `setMaxLength`). True when the value changed.
|
||||
pub(super) fn append_shortcut(
|
||||
selection: &mut clipboard::AppendSelection,
|
||||
board: &mut clipboard::SystemClipboard,
|
||||
id: clipboard::FieldId,
|
||||
value: &mut String,
|
||||
shortcut: EditShortcut,
|
||||
max_chars: usize,
|
||||
) -> bool {
|
||||
selection.apply(id, value, shortcut, board, |v, t| {
|
||||
clipboard::append_limited(v, t, max_chars)
|
||||
})
|
||||
}
|
||||
/// Shortcuts on the title, world creation and server screens.
|
||||
pub(super) fn title_shortcut(&mut self, shortcut: EditShortcut) {
|
||||
let Some(title) = self.title.as_mut() else {
|
||||
return;
|
||||
};
|
||||
if let Some((id, value, max)) = title.focused_field() {
|
||||
Self::append_shortcut(
|
||||
&mut self.text_selection,
|
||||
&mut self.clipboard,
|
||||
id,
|
||||
value,
|
||||
shortcut,
|
||||
max,
|
||||
);
|
||||
}
|
||||
}
|
||||
/// Typing or a backspace in an append-only field of the title screens:
|
||||
/// Select All's selection is replaced first.
|
||||
pub(super) fn title_edit(&mut self, typed: Option<&str>, backspace: bool) {
|
||||
let Some(title) = self.title.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let cleared = title
|
||||
.focused_field()
|
||||
.is_some_and(|(id, value, _)| self.text_selection.take(id, value));
|
||||
if backspace {
|
||||
if !cleared {
|
||||
title.backspace();
|
||||
}
|
||||
} else if let Some(text) = typed {
|
||||
title.type_text(text);
|
||||
}
|
||||
}
|
||||
/// Shortcuts in the creative inventory's search box (an `EditBox` of 50).
|
||||
pub(super) fn creative_search_shortcut(&mut self, shortcut: EditShortcut) {
|
||||
if Self::append_shortcut(
|
||||
&mut self.text_selection,
|
||||
&mut self.clipboard,
|
||||
("creative.search", 0),
|
||||
&mut self.creative_screen.search,
|
||||
shortcut,
|
||||
CREATIVE_SEARCH_CHARS,
|
||||
) {
|
||||
self.creative_screen.scroll = 0.0;
|
||||
}
|
||||
}
|
||||
/// Typing or a backspace in the creative search box.
|
||||
pub(super) fn creative_search_edit(&mut self, typed: Option<&str>, backspace: bool) {
|
||||
let search = &mut self.creative_screen.search;
|
||||
let cleared = self.text_selection.take(("creative.search", 0), search);
|
||||
if backspace {
|
||||
if !cleared {
|
||||
search.pop();
|
||||
}
|
||||
} else if let Some(text) = typed {
|
||||
clipboard::append_limited(search, text, CREATIVE_SEARCH_CHARS);
|
||||
}
|
||||
self.creative_screen.scroll = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// `CreativeModeInventoryScreen`'s search box: `setMaxLength(50)`.
|
||||
const CREATIVE_SEARCH_CHARS: usize = 50;
|
||||
@@ -589,7 +589,7 @@ impl Game<'_> {
|
||||
"pause_menu": self.pause_menu,
|
||||
"advanced_tooltips": self.settings.file.bool("advancedItemTooltips"),
|
||||
"pause_on_lost_focus": self.settings.file.bool("pauseOnLostFocus"),
|
||||
"clipboard": self.copied,
|
||||
"clipboard": self.clipboard.copied(),
|
||||
},
|
||||
"dimension": self.dim.id(),
|
||||
"creative": self.creative,
|
||||
|
||||
@@ -409,16 +409,8 @@ impl Game<'_> {
|
||||
}
|
||||
/// `KeyboardHandler.setClipboard`.
|
||||
fn set_clipboard(&mut self, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
if self.clipboard.is_none() {
|
||||
self.clipboard = arboard::Clipboard::new().ok();
|
||||
}
|
||||
if let Some(c) = self.clipboard.as_mut() {
|
||||
let _ = c.set_text(text.to_owned());
|
||||
}
|
||||
self.copied = Some(text.to_owned());
|
||||
use horizon_client::clipboard::Clipboard;
|
||||
self.clipboard.set(text);
|
||||
}
|
||||
/// The debug renderers' gizmos for a frame (`DebugRenderer.emitGizmos`).
|
||||
pub(super) fn debug_gizmos(&self, alpha: f32) -> Gizmos {
|
||||
|
||||
@@ -321,12 +321,17 @@ impl Game<'_> {
|
||||
return;
|
||||
};
|
||||
let ctrl = self.keys.contains(&winit::keyboard::KeyCode::ControlLeft);
|
||||
let shortcut = self.edit_shortcut(event);
|
||||
let Some(o) = self.world_options.as_mut() else {
|
||||
return;
|
||||
};
|
||||
if !o.port.focused {
|
||||
return;
|
||||
}
|
||||
if let Some(shortcut) = shortcut {
|
||||
o.port.shortcut(shortcut, &mut self.clipboard);
|
||||
return;
|
||||
}
|
||||
if !o.port.key(key, ctrl, false)
|
||||
&& !ctrl
|
||||
&& let Some(text) = &event.text
|
||||
|
||||
@@ -81,6 +81,15 @@ impl Game<'_> {
|
||||
self.ui.options = ui;
|
||||
self.handle_option_events(events)
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the options' search boxes; false when none has focus.
|
||||
pub(super) fn options_shortcut(
|
||||
&mut self,
|
||||
shortcut: horizon_client::clipboard::EditShortcut,
|
||||
) -> bool {
|
||||
self.ui
|
||||
.options
|
||||
.shortcut(&self.res.atlas, shortcut, &mut self.clipboard)
|
||||
}
|
||||
pub(super) fn options_scroll(&mut self, amount: f32) {
|
||||
self.ui.options.scroll_at(self.cursor, amount);
|
||||
}
|
||||
@@ -145,11 +154,13 @@ impl Game<'_> {
|
||||
r.set_mipmap_levels(s.mipmap_levels.max(0) as u32);
|
||||
r.set_world_post(s.horizon.resolution_scale as f32 / 100.0, s.horizon.fxaa);
|
||||
}
|
||||
if let Some(w) = &self.window {
|
||||
let want = s.fullscreen;
|
||||
if want != w.fullscreen().is_some() {
|
||||
w.set_fullscreen(want.then_some(Fullscreen::Borderless(None)));
|
||||
}
|
||||
// Window.updateFullscreenIfChanged: only a changed option asks the
|
||||
// window to change, so applying settings never undoes a fullscreen
|
||||
// the compositor set up.
|
||||
if let Some(w) = &self.window
|
||||
&& let Some(want) = self.fullscreen.request(s.fullscreen)
|
||||
{
|
||||
w.set_fullscreen(want.then_some(Fullscreen::Borderless(None)));
|
||||
}
|
||||
let mut volumes = s.volumes;
|
||||
volumes[0] = 1.0;
|
||||
@@ -171,6 +182,17 @@ impl Game<'_> {
|
||||
self.ui.chat.rescale(&self.res.atlas);
|
||||
}
|
||||
}
|
||||
/// `Window.updateFullscreenState`: a fullscreen change made by the window
|
||||
/// system is adopted by the option (`Minecraft.fullscreenStateChanged`).
|
||||
pub(super) fn observe_fullscreen(&mut self) {
|
||||
let Some(w) = &self.window else { return };
|
||||
if let Some(now) = self.fullscreen.observe(w.fullscreen().is_some())
|
||||
&& self.settings.fullscreen != now
|
||||
{
|
||||
self.settings.fullscreen = now;
|
||||
self.settings.save(&Self::options_path());
|
||||
}
|
||||
}
|
||||
/// F11 (key.fullscreen): Window.toggleFullScreen, remembered in options.txt.
|
||||
pub(super) fn toggle_fullscreen(&mut self) {
|
||||
self.settings.fullscreen = !self.settings.fullscreen;
|
||||
|
||||
@@ -165,9 +165,19 @@ impl Game<'_> {
|
||||
search.insert_text(text);
|
||||
}
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the search box.
|
||||
pub(super) fn recipe_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
|
||||
if let Some(search) = self.recipe_ui.search.as_mut() {
|
||||
search.shortcut(shortcut, &mut self.clipboard);
|
||||
}
|
||||
}
|
||||
/// A key press while the search box has focus.
|
||||
pub(super) fn recipe_key(&mut self, event: &winit::event::KeyEvent) {
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
if let Some(shortcut) = self.edit_shortcut(event) {
|
||||
self.recipe_shortcut(shortcut);
|
||||
return;
|
||||
}
|
||||
let ctrl = self.keys.contains(&KeyCode::ControlLeft);
|
||||
let shift = self.has_shift_down();
|
||||
let Some(search) = self.recipe_ui.search.as_mut() else {
|
||||
|
||||
@@ -2206,12 +2206,26 @@ impl Game<'_> {
|
||||
if event.state != winit::event::ElementState::Pressed {
|
||||
return;
|
||||
}
|
||||
let shortcut = self.edit_shortcut(event);
|
||||
let Some(r) = self.remote.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let Some(input) = r.chat_input.as_mut() else {
|
||||
return;
|
||||
};
|
||||
// ChatScreen's input: an EditBox of 256.
|
||||
const INPUT: horizon_client::clipboard::FieldId = ("remote.chat", 0);
|
||||
if let Some(shortcut) = shortcut {
|
||||
Self::append_shortcut(
|
||||
&mut self.text_selection,
|
||||
&mut self.clipboard,
|
||||
INPUT,
|
||||
input,
|
||||
shortcut,
|
||||
256,
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Logical keys: the text, not the scancode, decides (layouts, virtual keyboards).
|
||||
match &event.logical_key {
|
||||
Key::Named(NamedKey::Escape) => {
|
||||
@@ -2219,7 +2233,9 @@ impl Game<'_> {
|
||||
self.capture(true);
|
||||
}
|
||||
Key::Named(NamedKey::Backspace) => {
|
||||
input.pop();
|
||||
if !self.text_selection.take(INPUT, input) {
|
||||
input.pop();
|
||||
}
|
||||
}
|
||||
Key::Named(NamedKey::Enter) => {
|
||||
let message = r.chat_input.take().unwrap_or_default();
|
||||
@@ -2228,6 +2244,7 @@ impl Game<'_> {
|
||||
}
|
||||
_ => {
|
||||
if let Some(text) = &event.text {
|
||||
self.text_selection.take(INPUT, input);
|
||||
for c in text.chars().filter(|c| !c.is_control()) {
|
||||
if input.chars().count() < 256 {
|
||||
input.push(c);
|
||||
|
||||
@@ -5,7 +5,7 @@ use super::Game;
|
||||
use horizon_client::{
|
||||
containers::BlockEntity,
|
||||
hud::{HudBatch, rgba},
|
||||
signs::{SignData, wood_type},
|
||||
signs::wood_type,
|
||||
};
|
||||
use std::time::Instant;
|
||||
use winit::{
|
||||
@@ -21,13 +21,6 @@ pub(super) struct SignEditor {
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
fn edited_sign(&mut self) -> Option<&mut SignData> {
|
||||
let key = self.sign_edit.as_ref()?.pos;
|
||||
match self.world.block_entities.get_mut(&key) {
|
||||
Some(BlockEntity::Sign(sign)) => Some(sign),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// `SignBlock.openTextEdit` for the front side after placement.
|
||||
pub(super) fn open_sign_editor(&mut self, pos: (i32, i32, i32), front: bool) {
|
||||
self.sign_edit = Some(SignEditor {
|
||||
@@ -52,10 +45,15 @@ impl Game<'_> {
|
||||
if event.state != ElementState::Pressed {
|
||||
return;
|
||||
}
|
||||
if let Some(shortcut) = self.edit_shortcut(event) {
|
||||
self.sign_shortcut(shortcut);
|
||||
return;
|
||||
}
|
||||
let Some(edit) = self.sign_edit.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let front = edit.front;
|
||||
let id = sign_line_id(front, edit.line);
|
||||
match event.physical_key {
|
||||
PhysicalKey::Code(KeyCode::Escape) => self.close_sign_editor(),
|
||||
PhysicalKey::Code(KeyCode::ArrowUp) => edit.line = (edit.line + 3) & 3,
|
||||
@@ -63,14 +61,17 @@ impl Game<'_> {
|
||||
edit.line = (edit.line + 1) & 3
|
||||
}
|
||||
PhysicalKey::Code(KeyCode::Backspace) => {
|
||||
let line = edit.line;
|
||||
if let Some(sign) = self.edited_sign() {
|
||||
let (line, key) = (edit.line, edit.pos);
|
||||
if let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&key) {
|
||||
let text = if front {
|
||||
&mut sign.front
|
||||
} else {
|
||||
&mut sign.back
|
||||
};
|
||||
text.lines[line].pop();
|
||||
let value = &mut text.lines[line];
|
||||
if !self.text_selection.take(id, value) {
|
||||
value.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@@ -90,6 +91,7 @@ impl Game<'_> {
|
||||
} else {
|
||||
&mut sign.back
|
||||
};
|
||||
self.text_selection.take(id, &mut text.lines[line]);
|
||||
for ch in typed.chars().filter(|c| !c.is_control()) {
|
||||
let mut next = text.lines[line].clone();
|
||||
next.push(ch);
|
||||
@@ -102,6 +104,41 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `TextFieldHelper.keyPressed`'s shortcuts on the edited line: a paste
|
||||
/// (formatting and carriage returns removed) is taken whole or not at
|
||||
/// all, as the line must stay within the sign's width.
|
||||
fn sign_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
|
||||
let Some(edit) = self.sign_edit.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let (key, front, line) = (edit.pos, edit.front, edit.line);
|
||||
let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&key) else {
|
||||
return;
|
||||
};
|
||||
let max = sign.max_line_width();
|
||||
let text = if front {
|
||||
&mut sign.front
|
||||
} else {
|
||||
&mut sign.back
|
||||
};
|
||||
let atlas = &self.res.atlas;
|
||||
self.text_selection.apply(
|
||||
sign_line_id(front, line),
|
||||
&mut text.lines[line],
|
||||
shortcut,
|
||||
&mut self.clipboard,
|
||||
|value, pasted| {
|
||||
let next = format!(
|
||||
"{value}{}",
|
||||
horizon_client::clipboard::helper_clipboard_contents(pasted)
|
||||
);
|
||||
HudBatch::text_width(atlas, &next) <= max && {
|
||||
*value = next;
|
||||
true
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
/// Done button (`this.width / 2 - 100, this.height / 4 + 144`, 200x20).
|
||||
pub(super) fn sign_edit_click(&mut self, size: [u32; 2]) {
|
||||
let b = HudBatch::new(size);
|
||||
@@ -206,3 +243,8 @@ impl Game<'_> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The sign line being edited, for Select All.
|
||||
fn sign_line_id(front: bool, line: usize) -> horizon_client::clipboard::FieldId {
|
||||
(if front { "sign.front" } else { "sign.back" }, line)
|
||||
}
|
||||
|
||||
@@ -1158,17 +1158,41 @@ impl Game<'_> {
|
||||
let Some(s) = self.station.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let cleared = self.text_selection.take(ANVIL_NAME, &mut s.name);
|
||||
if backspace {
|
||||
s.name.pop();
|
||||
if !cleared {
|
||||
s.name.pop();
|
||||
}
|
||||
} else if let Some(t) = text {
|
||||
for c in t.chars().filter(|c| !c.is_control()) {
|
||||
if s.name.chars().count() < 50 {
|
||||
if s.name.chars().count() < ANVIL_NAME_CHARS {
|
||||
s.name.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.station_changed();
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the anvil's rename box (an `EditBox` of 50) or the
|
||||
/// recipe book's search box.
|
||||
pub(super) fn station_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
|
||||
if self.recipe_search_focused() {
|
||||
self.recipe_shortcut(shortcut);
|
||||
return;
|
||||
}
|
||||
let Some(s) = self.station.as_mut() else {
|
||||
return;
|
||||
};
|
||||
if Self::append_shortcut(
|
||||
&mut self.text_selection,
|
||||
&mut self.clipboard,
|
||||
ANVIL_NAME,
|
||||
&mut s.name,
|
||||
shortcut,
|
||||
ANVIL_NAME_CHARS,
|
||||
) {
|
||||
self.station_changed();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- drawing
|
||||
|
||||
@@ -2063,3 +2087,7 @@ impl Game<'_> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The anvil's rename box (`AnvilScreen`: `setMaxLength(50)`).
|
||||
const ANVIL_NAME: horizon_client::clipboard::FieldId = ("anvil.name", 0);
|
||||
const ANVIL_NAME_CHARS: usize = 50;
|
||||
|
||||
@@ -25,6 +25,7 @@ pub mod camera;
|
||||
pub mod cauldron;
|
||||
pub mod chat;
|
||||
pub mod chunk;
|
||||
pub mod clipboard;
|
||||
pub mod clock;
|
||||
pub mod clouds;
|
||||
pub mod collisions;
|
||||
|
||||
@@ -578,6 +578,16 @@ impl Screen {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// The focused text field, its id and maximum length (copy and paste).
|
||||
pub fn focused_text(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
|
||||
let id = match self {
|
||||
Self::Edit { focus: 0, .. } => ("server.name", 0),
|
||||
Self::Edit { .. } => ("server.address", 0),
|
||||
Self::Direct { .. } => ("server.direct", 0),
|
||||
_ => return None,
|
||||
};
|
||||
self.focused_field().map(|f| (id, f, 128))
|
||||
}
|
||||
pub fn type_text(&mut self, text: &str) {
|
||||
if let Some(field) = self.focused_field() {
|
||||
for c in text.chars().filter(|c| !c.is_control()) {
|
||||
|
||||
@@ -1642,6 +1642,31 @@ impl OptionsUi {
|
||||
self.scroll = 0.0;
|
||||
}
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the language or pack search box; false when neither
|
||||
/// is shown with the focus.
|
||||
pub fn shortcut(
|
||||
&mut self,
|
||||
atlas: &HudAtlas,
|
||||
shortcut: crate::clipboard::EditShortcut,
|
||||
clipboard: &mut dyn crate::clipboard::Clipboard,
|
||||
) -> bool {
|
||||
if self.selected_key.is_some() {
|
||||
return false;
|
||||
}
|
||||
if self.page() == Page::Packs
|
||||
&& let Some(p) = &mut self.pack_screen
|
||||
{
|
||||
let m = crate::screen_draw::Measure(&atlas.font);
|
||||
return p.shortcut(&m, shortcut, clipboard);
|
||||
}
|
||||
if self.page() == Page::Language {
|
||||
if self.language_search.shortcut(shortcut, clipboard) {
|
||||
self.scroll = 0.0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
/// Keys while the options are open. Returns events; `handled` false lets
|
||||
/// Escape fall through.
|
||||
pub fn key(
|
||||
|
||||
@@ -673,6 +673,21 @@ impl PackScreen {
|
||||
self.filter(None);
|
||||
}
|
||||
}
|
||||
/// Ctrl+A/C/V/X in the search box; false when it does not have the focus.
|
||||
pub fn shortcut(
|
||||
&mut self,
|
||||
m: &Measure,
|
||||
shortcut: crate::clipboard::EditShortcut,
|
||||
clipboard: &mut dyn crate::clipboard::Clipboard,
|
||||
) -> bool {
|
||||
if !self.search_focused() {
|
||||
return false;
|
||||
}
|
||||
if self.search.shortcut(m, shortcut, clipboard) {
|
||||
self.filter(None);
|
||||
}
|
||||
true
|
||||
}
|
||||
/// Backspace in the search box.
|
||||
pub fn erase(&mut self, m: &Measure, count: isize) {
|
||||
if self.confirm.is_none()
|
||||
|
||||
@@ -476,6 +476,23 @@ fn chars(s: &str) -> usize {
|
||||
fn sub(s: &str, from: usize, to: usize) -> String {
|
||||
s.chars().skip(from).take(to.saturating_sub(from)).collect()
|
||||
}
|
||||
/// A text field with the font that scrolls it.
|
||||
struct MeasuredField<'a, 'm>(&'a mut TextField, &'a Measure<'m>);
|
||||
impl crate::clipboard::Editable for MeasuredField<'_, '_> {
|
||||
fn select_all(&mut self) {
|
||||
let len = chars(&self.0.value);
|
||||
self.0.move_cursor_to(self.1, len, false);
|
||||
self.0.set_highlight(self.1, 0);
|
||||
}
|
||||
fn selected_text(&self) -> String {
|
||||
self.0.selected_text()
|
||||
}
|
||||
fn insert(&mut self, text: &str) -> bool {
|
||||
let before = self.0.value.clone();
|
||||
self.0.insert_text(self.1, text);
|
||||
self.0.value != before
|
||||
}
|
||||
}
|
||||
impl TextField {
|
||||
pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
|
||||
Self {
|
||||
@@ -668,6 +685,23 @@ impl TextField {
|
||||
}
|
||||
Some(false)
|
||||
}
|
||||
/// Ctrl+A/C/V/X (`EditBox.keyPressed`). True when the value changed.
|
||||
pub fn shortcut(
|
||||
&mut self,
|
||||
m: &Measure,
|
||||
shortcut: crate::clipboard::EditShortcut,
|
||||
clipboard: &mut dyn crate::clipboard::Clipboard,
|
||||
) -> bool {
|
||||
crate::clipboard::apply(&mut MeasuredField(self, m), shortcut, clipboard)
|
||||
}
|
||||
/// `getHighlighted`.
|
||||
pub fn selected_text(&self) -> String {
|
||||
sub(
|
||||
&self.value,
|
||||
self.cursor.min(self.highlight),
|
||||
self.cursor.max(self.highlight),
|
||||
)
|
||||
}
|
||||
/// `getScreenX`: where a character index is drawn, from the box's left.
|
||||
pub fn screen_x(&self, m: &Measure, index: usize) -> i32 {
|
||||
if index > chars(&self.value) {
|
||||
|
||||
@@ -541,6 +541,20 @@ impl Title {
|
||||
_ => Action::None,
|
||||
}
|
||||
}
|
||||
/// The focused text field, its id and maximum length (copy and paste).
|
||||
pub fn focused_field(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
|
||||
match &mut self.mode {
|
||||
Mode::Multiplayer(screen) => screen.focused_text(),
|
||||
Mode::Create {
|
||||
name, seed, focus, ..
|
||||
} => match focus {
|
||||
0 => Some((("world.name", 0), name, 32)),
|
||||
1 => Some((("world.seed", 0), seed, 32)),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn type_text(&mut self, text: &str) {
|
||||
if let Mode::Multiplayer(screen) = &mut self.mode {
|
||||
screen.type_text(text);
|
||||
|
||||
@@ -520,6 +520,20 @@ fn byte_index(s: &str, chars: usize) -> usize {
|
||||
pub fn char_len(s: &str) -> usize {
|
||||
s.chars().count()
|
||||
}
|
||||
impl crate::clipboard::Editable for EditBox {
|
||||
fn select_all(&mut self) {
|
||||
self.move_cursor_to_end(false);
|
||||
self.highlight = 0;
|
||||
}
|
||||
fn selected_text(&self) -> String {
|
||||
EditBox::selected_text(self)
|
||||
}
|
||||
fn insert(&mut self, text: &str) -> bool {
|
||||
let before = self.value.clone();
|
||||
self.insert_text(text);
|
||||
self.value != before
|
||||
}
|
||||
}
|
||||
impl EditBox {
|
||||
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
|
||||
Self {
|
||||
@@ -576,10 +590,7 @@ impl EditBox {
|
||||
/// EditBox.insertText: replace the selection, respecting the maximum length.
|
||||
pub fn insert_text(&mut self, text: &str) {
|
||||
let (a, b) = self.selected();
|
||||
let filtered: String = text
|
||||
.chars()
|
||||
.filter(|c| !c.is_control() && *c != '§')
|
||||
.collect();
|
||||
let filtered = crate::clipboard::filter_text(text, false);
|
||||
let room = self
|
||||
.max_length
|
||||
.saturating_sub(char_len(&self.value) - (b - a));
|
||||
@@ -702,6 +713,17 @@ impl EditBox {
|
||||
}
|
||||
true
|
||||
}
|
||||
/// Ctrl+A/C/V/X (`EditBox.keyPressed`). True when the value changed.
|
||||
pub fn shortcut(
|
||||
&mut self,
|
||||
shortcut: crate::clipboard::EditShortcut,
|
||||
clipboard: &mut dyn crate::clipboard::Clipboard,
|
||||
) -> bool {
|
||||
let before = self.value.clone();
|
||||
crate::clipboard::apply(self, shortcut, clipboard);
|
||||
self.reset_blink();
|
||||
self.value != before
|
||||
}
|
||||
/// The first shown character after scrolling to the cursor, and the
|
||||
/// milliseconds since the field was focused (the cursor's blink).
|
||||
pub fn view_state(&mut self, atlas: &HudAtlas) -> (usize, u64) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Window sizing and frame pacing of 26.3: `Window.calculateScale` and
|
||||
//! `setGuiScale`, `MouseHandler`'s scaled positions, `FramerateLimitTracker`,
|
||||
//! `FramerateLimiter` and the vsync present-mode choice.
|
||||
//! `FramerateLimiter`, the vsync present-mode choice and the fullscreen
|
||||
//! request/state pair.
|
||||
|
||||
/// `Window.calculateScale`: the largest scale that leaves at least 320x240
|
||||
/// GUI pixels, capped by `max_scale` (0 = automatic); an odd result is bumped
|
||||
@@ -263,10 +264,85 @@ impl FramerateLimiter {
|
||||
}
|
||||
}
|
||||
|
||||
/// blaze3d `Window`'s `fullscreenRequested` / `fullscreen` pair. The window
|
||||
/// system has the last word: a change it makes on its own (a compositor
|
||||
/// shortcut, a window rule) is adopted by the Fullscreen option
|
||||
/// (`Window.updateFullscreenState` -> `Minecraft.fullscreenStateChanged`), and
|
||||
/// the window is only asked to change when the option itself changes
|
||||
/// (`Window.updateFullscreenIfChanged`), never re-forced to the option.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct FullscreenState {
|
||||
/// The state the window system last reported (`Window.fullscreen`).
|
||||
actual: bool,
|
||||
/// The state last asked of the window system, or adopted from it.
|
||||
requested: bool,
|
||||
}
|
||||
|
||||
impl FullscreenState {
|
||||
/// A window created windowed or, when `requested`, asked to be fullscreen.
|
||||
pub fn new(requested: bool) -> Self {
|
||||
Self {
|
||||
actual: false,
|
||||
requested,
|
||||
}
|
||||
}
|
||||
|
||||
/// `Window.updateFullscreenIfChanged`: the state to ask of the window
|
||||
/// when the option `wanted` differs from what was last asked or adopted.
|
||||
pub fn request(&mut self, wanted: bool) -> Option<bool> {
|
||||
(wanted != self.requested).then(|| {
|
||||
self.requested = wanted;
|
||||
wanted
|
||||
})
|
||||
}
|
||||
|
||||
/// `Window.updateFullscreenState`: the window now reports `now`; returns
|
||||
/// it when it changed, for the option to adopt.
|
||||
pub fn observe(&mut self, now: bool) -> Option<bool> {
|
||||
(now != self.actual).then(|| {
|
||||
self.actual = now;
|
||||
self.requested = now;
|
||||
now
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fullscreen_follows_the_option_only_when_it_changes() {
|
||||
let mut f = FullscreenState::new(false);
|
||||
// Settings applied again with nothing changed: the window is left alone.
|
||||
assert_eq!(f.request(false), None);
|
||||
// The compositor makes the window fullscreen on its own: the option adopts it,
|
||||
// and applying the settings afterwards does not undo it.
|
||||
assert_eq!(f.observe(true), Some(true));
|
||||
assert_eq!(f.request(true), None);
|
||||
assert_eq!(f.observe(true), None);
|
||||
// F11: one request, not repeated while the compositor catches up.
|
||||
assert_eq!(f.request(false), Some(false));
|
||||
assert_eq!(f.request(false), None);
|
||||
assert_eq!(f.observe(false), Some(false));
|
||||
assert_eq!(f.request(false), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fullscreen_requests_changed_before_the_compositor_answers() {
|
||||
// Created fullscreen from options.txt.
|
||||
let mut f = FullscreenState::new(true);
|
||||
assert_eq!(f.request(true), None);
|
||||
assert_eq!(f.observe(true), Some(true));
|
||||
// Toggled twice before any configure: the second request cancels the first.
|
||||
assert_eq!(f.request(false), Some(false));
|
||||
assert_eq!(f.request(true), Some(true));
|
||||
assert_eq!(f.observe(true), None);
|
||||
// A compositor that refuses leaves the option where the player put it.
|
||||
assert_eq!(f.request(false), Some(false));
|
||||
assert_eq!(f.request(false), None);
|
||||
}
|
||||
|
||||
struct Virtual {
|
||||
now: i64,
|
||||
oversleep: i64,
|
||||
|
||||
Reference in New Issue
Block a user