Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/nsh-base/src/data_table/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ impl Dt {

pub(crate) fn copy_selection(&self) {
if let Some(text) = self.selection_text() {
drop(set_clipboard(text));
drop(set_clipboard_text(text));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/nsh-base/src/editor/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ impl Ed {
st.editing(1, |e| e.copy())
};
*self.line_copy.borrow_mut() = line_wise.then(|| text.clone());
drop(set_clipboard(text));
drop(set_clipboard_text(text));
}

pub(crate) fn cut(&self) {
Expand All @@ -1082,7 +1082,7 @@ impl Ed {
st.editing(1, |e| e.cut())
};
*self.line_copy.borrow_mut() = line_wise.then(|| text.clone());
drop(set_clipboard(text));
drop(set_clipboard_text(text));
self.after(outcome);
}

Expand All @@ -1100,7 +1100,7 @@ impl Ed {
pub(crate) fn paste_clipboard(&self) {
let ed = self.clone();
spawn(async move {
let text = clipboard_string().await;
let text = clipboard_text().await;
ed.paste(&text);
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nsh-base/src/text_view/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ impl Cx {
.label(label)
.on_press(move || {
if copy {
drop(set_clipboard(event.code.code.clone()));
drop(set_clipboard_text(event.code.code.clone()));
let _ = done.spring(
1.0,
Spring {
Expand Down Expand Up @@ -2631,7 +2631,7 @@ fn context_menu(
press_link(scope, links, url, Modifiers::default())
}
(TextViewMenuItem::COPY_LINK, Some(url)) => {
drop(set_clipboard(url.to_string()))
drop(set_clipboard_text(url.to_string()))
}
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nsh-core/src/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21593,7 +21593,7 @@ mod components {
view()
.child(text(move || format!("pasted {}", pasted.get())))
.child(view().h(px(10.0)).on_press(move |_: &()| {
let (has, text) = (clipboard_has_string(), clipboard_string());
let (has, text) = (clipboard_has_text(), clipboard_text());
spawn(async move {
let value = if has.await { text.await } else { "-".into() };
pasted.set(value);
Expand Down Expand Up @@ -21687,7 +21687,7 @@ mod components {
.child(text(move || format!("copied {}", results.get())))
.child(view().h(px(10.0)).on_press(move |_: &()| {
let (first, second) = (
set_clipboard("a"),
set_clipboard_text("a"),
set_clipboard_item(ClipboardItem {
text: "b".into(),
html: Some("<b>b</b>".into()),
Expand Down
40 changes: 36 additions & 4 deletions crates/nsh-core/src/element/ambient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ pub fn open_url(url: impl Into<String>) -> Answer<bool> {
/// answer says whether the system clipboard took it
/// ([`ClipboardWrite`]); drop it when that does not matter, the write
/// happens either way.
pub fn set_clipboard(text: impl Into<String>) -> Answer<ClipboardWrite> {
pub fn set_clipboard_text(text: impl Into<String>) -> Answer<ClipboardWrite> {
let (answer, action) = super::services::set_clipboard(text.into());
act(action);
answer
Expand All @@ -873,7 +873,7 @@ pub fn set_clipboard_item(item: ClipboardItem) -> Answer<ClipboardWrite> {

/// The text on the system clipboard, `""` when it holds none (TS:
/// `Clipboard.readText`), to await in a task.
pub fn clipboard_string() -> Answer<String> {
pub fn clipboard_text() -> Answer<String> {
let (answer, action) = super::services::clipboard_string();
act(action);
answer
Expand All @@ -889,12 +889,39 @@ pub(crate) fn clipboard_checked_string() -> Answer<Option<String>> {
}

/// Whether the system clipboard holds text (TS: `Clipboard.hasText`).
pub fn clipboard_has_string() -> Answer<bool> {
pub fn clipboard_has_text() -> Answer<bool> {
let (answer, action) = super::services::clipboard_has_string();
act(action);
answer
}

/// Renamed [`set_clipboard_text`], after TS's `Clipboard.writeText`.
#[deprecated(
since = "0.9.0",
note = "renamed `set_clipboard_text`, after TS's `Clipboard.writeText`"
)]
pub fn set_clipboard(text: impl Into<String>) -> Answer<ClipboardWrite> {
set_clipboard_text(text)
}

/// Renamed [`clipboard_text`], after TS's `Clipboard.readText`.
#[deprecated(
since = "0.9.0",
note = "renamed `clipboard_text`, after TS's `Clipboard.readText`"
)]
pub fn clipboard_string() -> Answer<String> {
clipboard_text()
}

/// Renamed [`clipboard_has_text`], after TS's `Clipboard.hasText`.
#[deprecated(
since = "0.9.0",
note = "renamed `clipboard_has_text`, after TS's `Clipboard.hasText`"
)]
pub fn clipboard_has_string() -> Answer<bool> {
clipboard_has_text()
}

/// Puts the image of a base64 `data:` URI, or of the URL
/// [`clipboard_image`] gave, on the system clipboard (TS:
/// `Clipboard.writeImage`). Where the platform puts no images on the
Expand Down Expand Up @@ -1989,7 +2016,12 @@ pub fn on_key_char(options: KeyOptions, handler: impl Fn(&str) + 'static) {
let keys = executor().keys.clone();
let id = keys.borrow_mut().add(
|id| {
crate::keymap::KeyBinding::new(id, stroke, crate::keymap::When::NotEditing, options.repeat)
crate::keymap::KeyBinding::new(
id,
stroke,
crate::keymap::When::NotEditing,
options.repeat,
)
},
"".into(),
handler,
Expand Down
16 changes: 14 additions & 2 deletions crates/nsh-core/src/element/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub enum FunctionCall {
Op(fn(&dyn JsHost) -> Result<OpCall, Thrown>),
/// As a task started in order with the UI's ops (its arguments
/// converted in the JS call): it reaches the platform with the same free
/// functions views use (`dialog`, `clipboard_string`, ...).
/// functions views use (`dialog`, `clipboard_text`, ...).
Async(AsyncCall),
/// In the JS call, returning a task's start ([`super::Later`]): JS gets
/// a promise of the task, rejected with what the call refuses (a
Expand Down Expand Up @@ -376,7 +376,7 @@ impl ModuleCx {

/// Puts `text` on the system clipboard (TS: `Clipboard.writeText`).
/// The answer says whether the system clipboard took it.
pub fn set_clipboard(
pub fn set_clipboard_text(
&mut self,
text: impl Into<String>,
) -> super::Answer<super::ClipboardWrite> {
Expand All @@ -385,6 +385,18 @@ impl ModuleCx {
answer
}

/// Renamed [`Self::set_clipboard_text`], after TS's `Clipboard.writeText`.
#[deprecated(
since = "0.9.0",
note = "renamed `set_clipboard_text`, after TS's `Clipboard.writeText`"
)]
pub fn set_clipboard(
&mut self,
text: impl Into<String>,
) -> super::Answer<super::ClipboardWrite> {
self.set_clipboard_text(text)
}

/// Puts `item`, its text and its HTML, on the system clipboard (TS:
/// `Clipboard.write`).
/// The answer says whether the system clipboard took it.
Expand Down
43 changes: 22 additions & 21 deletions crates/nsh-core/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ pub use crate::element::{
load_sound,
};
#[allow(deprecated)]
pub use crate::element::{Modal, Screen, Stack};
pub use crate::element::{
Modal, Screen, Stack, clipboard_has_string, clipboard_string, set_clipboard,
};
#[cfg(not(target_arch = "wasm32"))]
pub use crate::element::{background, blocking};
/// The app's host ([`crate::host_channel`]; TS: `nsh`'s `host`).
Expand All @@ -283,26 +285,25 @@ pub use crate::{
Reactive, ReadSignal, Resource, ResourceInfo, ResourceState, Scope, Signal, SignalEquals,
Spawned, Stored, after_answer, animate_layout, animated_value, announce_for_accessibility,
app_info, app_url, apple_keys, batch, can_pick_screen_color, catch_error, cleanup,
clipboard_files, clipboard_has_image, clipboard_has_string, clipboard_image,
clipboard_string, close_all_notifications, close_notification, color, component,
component_ref, computed, dark, data_dir, dismiss_keyboard, dynamic, each, effect,
element_ref, engine_context, fetch, font_scale, fps_enabled, global, haptic,
horizontal_size_class, idiom, image_size, input_modality, is_catalyst, is_desktop,
key_label, keyboard_duration, keyboard_height, keystroke_label, lifecycle, locale,
locale_settings, log_file, measure_text, motion, move_to_trash, network,
next_app_url_change, next_frame, next_notification_press, next_open_files,
next_second_instance, next_url_scheme_open, next_url_scheme_open_for,
notification_permission, on_back_press, on_back_progress, on_key, on_key_char, on_key_with, open_path,
open_url, os, os_version, perf_stats, pick_files, pick_images, pick_paths, pick_save_path,
pick_screen_color, pixel_ratio, platform_name, prefetch_image, provide_context,
reads_active_descendants, reduce_motion, register_font, request_animation_frame,
request_notification_permission, reserved_regions, resource, resource_with_value,
reveal_path, round_to_nearest_pixel, run_on_main_thread, safe_area, screen_reader,
set_app_url, set_app_url_with, set_badge, set_clipboard, set_clipboard_image,
set_clipboard_item, set_color_scheme, set_fps_enabled, set_fps_mode, set_interval,
set_timeout, set_tokens, shaders_available, show_notification, signal, signal_with, spawn,
stored, text_metrics, timer, timer_until, tokens, try_apple_keys, try_global, try_os,
untracked, use_context, vertical_size_class, wake_count, watch_scroll,
clipboard_files, clipboard_has_image, clipboard_has_text, clipboard_image, clipboard_text,
close_all_notifications, close_notification, color, component, component_ref, computed,
dark, data_dir, dismiss_keyboard, dynamic, each, effect, element_ref, engine_context,
fetch, font_scale, fps_enabled, global, haptic, horizontal_size_class, idiom, image_size,
input_modality, is_catalyst, is_desktop, key_label, keyboard_duration, keyboard_height,
keystroke_label, lifecycle, locale, locale_settings, log_file, measure_text, motion,
move_to_trash, network, next_app_url_change, next_frame, next_notification_press,
next_open_files, next_second_instance, next_url_scheme_open, next_url_scheme_open_for,
notification_permission, on_back_press, on_back_progress, on_key, on_key_char, on_key_with,
open_path, open_url, os, os_version, perf_stats, pick_files, pick_images, pick_paths,
pick_save_path, pick_screen_color, pixel_ratio, platform_name, prefetch_image,
provide_context, reads_active_descendants, reduce_motion, register_font,
request_animation_frame, request_notification_permission, reserved_regions, resource,
resource_with_value, reveal_path, round_to_nearest_pixel, run_on_main_thread, safe_area,
screen_reader, set_app_url, set_app_url_with, set_badge, set_clipboard_image,
set_clipboard_item, set_clipboard_text, set_color_scheme, set_fps_enabled, set_fps_mode,
set_interval, set_timeout, set_tokens, shaders_available, show_notification, signal,
signal_with, spawn, stored, text_metrics, timer, timer_until, tokens, try_apple_keys,
try_global, try_os, untracked, use_context, vertical_size_class, wake_count, watch_scroll,
web_evaluate_available, web_input_available, web_view_available, when, window,
},
host_channel::{HostCallOptions, HostError},
Expand Down
4 changes: 2 additions & 2 deletions crates/nsh-core/src/runtime/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Clipboard {
sys: System,
) -> Result<crate::element::ClipboardWrite, JsError> {
granted(&sys)?;
Ok(crate::prelude::set_clipboard(text).await)
Ok(crate::prelude::set_clipboard_text(text).await)
}

/// Puts `item` on the clipboard, replacing what it holds: its
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Clipboard {
/// notice).
pub(crate) async fn has_text(sys: System) -> Result<bool, JsError> {
granted(&sys)?;
Ok(crate::prelude::clipboard_has_string().await)
Ok(crate::prelude::clipboard_has_text().await)
}

/// Puts the image of `src`, a base64 `data:` URL or the `url`
Expand Down
2 changes: 1 addition & 1 deletion crates/nsh-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ fn module_cx(ty: &Type) -> bool {
/// `undefined` (`void`) when it returns nothing (JS calls it and moves on,
/// and a wrong argument throws);
/// an `async fn` runs as a task of the core with the free functions views
/// use (`dialog(..)`, `clipboard_string()`, ...), a `Promise` (of `void`
/// use (`dialog(..)`, `clipboard_text()`, ...), a `Promise` (of `void`
/// too). Parameters
/// are [`JsValue`] types (a trailing `Option` is optional), instances
/// (`Instance<T>`) and JS functions (`Callback<A, R>`; `#[args(name,
Expand Down
4 changes: 2 additions & 2 deletions crates/nsh-testing/src/headless/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3187,7 +3187,7 @@ mod tests {
view()
.on_press(move || {
count.update(|c| *c += 1);
drop(set_clipboard(format!("copied {}", count.get_untracked())));
drop(set_clipboard_text(format!("copied {}", count.get_untracked())));
})
.child(text("Copy"))
}
Expand Down Expand Up @@ -3233,7 +3233,7 @@ mod tests {
)
.child(
view()
.on_press(|| drop(set_clipboard("plain")))
.on_press(|| drop(set_clipboard_text("plain")))
.child(text("Plain")),
)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/api-naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ release (item 3 of the API review); where code differs from one, the rule wins a
not granted. `null` stays an answer to a query with nothing there (`groupOf`, `posAtCoords` over nothing,
`Storage.getItem` of an absent key, `ViewRef.measure()` of a view not in the tree), and a cancel resolves empty.
`host.post` is fire-and-forget, as an event is: with no embedding app it is dropped.
- It is the rule of what JS calls: Rust's own API (`clipboard_string`, `pick_images`, `open_url`) answers a
- It is the rule of what JS calls: Rust's own API (`clipboard_text`, `pick_images`, `open_url`) answers a
refusal as it answers a cancel, as GPUI's do.
- A command that runs at the commit (a component's or ref's method with no result: `DockArea.selectTab`,
`Diff.goToHunk`) cannot throw: given an unknown id or an index out of range it does nothing and logs a warning;
Expand Down
6 changes: 3 additions & 3 deletions docs/dev/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ reasons: `reactive-components.md`.
| a view's `direction` style | `.direction(LayoutDirection::Rtl)` (`Styled`); the surface's direction follows the user's language |
| `import logo from "./logo@2x.png"`, `import font from "./a.ttf"` | `const LOGO: ImageAsset = asset!("logo@2x.png");` (`image(LOGO)` lays out at its natural size), `const A: FontAsset = asset!("a.ttf");` (`.font_family(A.family)`), with `registry.assets(assets!())` (below) |
| `Image.getSize(src)`, `Image.prefetch(src)` | `image_size(src)` / `prefetch_image(src)`: answers to await in a task |
| `Clipboard.writeText(text)`, `Clipboard.write({ text, html })`, `await Clipboard.readText()` / `hasText()`, `writeImage(src)`, `readImage()` / `hasImage()`, `readFiles()` (`nsh`'s `Clipboard`) | `set_clipboard(text)`, `set_clipboard_item(item)` (a `ClipboardItem`: one item as a plain object of its formats, `text` and `html`, named as GPUI's and the DOM's), `clipboard_string()` / `clipboard_has_string()`, `set_clipboard_image(uri)`, `clipboard_image()` (an `Option<ClipboardImage>`) / `clipboard_has_image()`, `clipboard_files()`: answers to await in a task (the app's `capabilities.clipboard` applies as for JS) |
| `Clipboard.writeText(text)`, `Clipboard.write({ text, html })`, `await Clipboard.readText()` / `hasText()`, `writeImage(src)`, `readImage()` / `hasImage()`, `readFiles()` (`nsh`'s `Clipboard`) | `set_clipboard_text(text)`, `set_clipboard_item(item)` (a `ClipboardItem`: one item as a plain object of its formats, `text` and `html`, named as GPUI's and the DOM's), `clipboard_text()` / `clipboard_has_text()`, `set_clipboard_image(uri)`, `clipboard_image()` (an `Option<ClipboardImage>`) / `clipboard_has_image()`, `clipboard_files()`: answers to await in a task (the app's `capabilities.clipboard` applies as for JS) |
| `await Storage.getItem(k)`, `setItem(k, v)`, `removeItem(k)`, `keys()`, `clear()` (`nsh`'s `Storage`) | `nsh_core::storage::Storage::get_item(k)`, `set_item(k, v)` (`Err(StorageFull)` past the quota), `remove_item(k)`, `get_all_keys()`, `clear()`: synchronous (the entries are in memory on the engine thread), and the same entries the app's JS uses, so a TSX entry and a Rust entry of one app read each other's data |
| `Storage.on("change", listener)` (`nsh`'s `Storage`) | `nsh_core::storage::Storage::on_change(f)`: the changes another window, view or (web) tab made, under the current owner; this engine's own changes do not come |
| `announce(message)`, `setAccessibilityFocus(ref)` (`nsh`'s accessibility API) | `announce_for_accessibility(msg)`, `note.set_accessibility_focus()` (an `ElementRef`) |
Expand Down Expand Up @@ -430,8 +430,8 @@ reasons: `reactive-components.md`.
- **Platform services** are free functions (`element/ambient.rs`, exported from `element` and the prelude): `dialog(Dialog)` (deprecated for apps in favor of
`nsh_base::alert_dialog()`, which calls it) shows an alert or action sheet and returns a future of the index
chosen (-1: dismissed; the answer is the root's `Dialog` event, which the core resolves; JS never sees it); `dismiss_keyboard()`, `animate_layout(..)`, `announce_for_accessibility(..)`,
`set_color_scheme(..)`, the clipboard (`set_clipboard`, `clipboard_string`,
`clipboard_has_string`), the image information (`image_size`, `prefetch_image`), display frames
`set_color_scheme(..)`, the clipboard (`set_clipboard_text`, `clipboard_text`,
`clipboard_has_text`), the image information (`image_size`, `prefetch_image`), display frames
(`request_animation_frame`, `next_frame`: the core keeps the waiting tasks' answers, the engine asks the platform
for a frame while one waits, `MountSink::request_frame`, and delivers it at `EngineControl::frame`), and the status
bar (below); they are actions the core applies in order with the view's other changes (`element/services.rs`,
Expand Down
Loading