android changes taken from ibaryshnikov
This commit is contained in:
parent
50cc94d944
commit
a6f4b5ac0a
7 changed files with 130 additions and 31 deletions
|
|
@ -88,6 +88,10 @@ pub enum Action {
|
||||||
/// The amount of lines to scroll.
|
/// The amount of lines to scroll.
|
||||||
lines: i32,
|
lines: i32,
|
||||||
},
|
},
|
||||||
|
/// Editor gained focus
|
||||||
|
Focus,
|
||||||
|
/// Editor lost focus
|
||||||
|
Blur,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Action {
|
impl Action {
|
||||||
|
|
|
||||||
|
|
@ -164,25 +164,6 @@ impl Theme {
|
||||||
|
|
||||||
impl Default for Theme {
|
impl Default for Theme {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
#[cfg(feature = "auto-detect-theme")]
|
|
||||||
{
|
|
||||||
use std::sync::LazyLock;
|
|
||||||
|
|
||||||
static DEFAULT: LazyLock<Theme> = LazyLock::new(|| {
|
|
||||||
match dark_light::detect()
|
|
||||||
.unwrap_or(dark_light::Mode::Unspecified)
|
|
||||||
{
|
|
||||||
dark_light::Mode::Dark => Theme::Dark,
|
|
||||||
dark_light::Mode::Light | dark_light::Mode::Unspecified => {
|
|
||||||
Theme::Light
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
DEFAULT.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "auto-detect-theme"))]
|
|
||||||
Theme::Light
|
Theme::Light
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -159,16 +159,31 @@ pub fn font_system() -> &'static RwLock<FontSystem> {
|
||||||
static FONT_SYSTEM: OnceLock<RwLock<FontSystem>> = OnceLock::new();
|
static FONT_SYSTEM: OnceLock<RwLock<FontSystem>> = OnceLock::new();
|
||||||
|
|
||||||
FONT_SYSTEM.get_or_init(|| {
|
FONT_SYSTEM.get_or_init(|| {
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut raw = cosmic_text::FontSystem::new_with_fonts([
|
||||||
|
cosmic_text::fontdb::Source::Binary(Arc::new(
|
||||||
|
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
|
||||||
|
)),
|
||||||
|
#[cfg(all(target_arch = "wasm32", feature = "fira-sans"))]
|
||||||
|
cosmic_text::fontdb::Source::Binary(Arc::new(
|
||||||
|
include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
|
||||||
|
)),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Load system fonts for Android
|
||||||
|
// https://github.com/pop-os/cosmic-text/issues/243#issue-2189977938
|
||||||
|
#[cfg(target_os = "android")]
|
||||||
|
{
|
||||||
|
raw.db_mut().load_fonts_dir("/system/fonts");
|
||||||
|
raw.db_mut().set_sans_serif_family("Roboto");
|
||||||
|
raw.db_mut().set_serif_family("Noto Serif");
|
||||||
|
raw.db_mut().set_monospace_family("Droid Sans Mono"); // Cutive Mono looks more printer-like
|
||||||
|
raw.db_mut().set_cursive_family("Dancing Script");
|
||||||
|
raw.db_mut().set_fantasy_family("Dancing Script");
|
||||||
|
}
|
||||||
|
|
||||||
RwLock::new(FontSystem {
|
RwLock::new(FontSystem {
|
||||||
raw: cosmic_text::FontSystem::new_with_fonts([
|
raw,
|
||||||
cosmic_text::fontdb::Source::Binary(Arc::new(
|
|
||||||
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
|
|
||||||
)),
|
|
||||||
#[cfg(feature = "fira-sans")]
|
|
||||||
cosmic_text::fontdb::Source::Binary(Arc::new(
|
|
||||||
include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
|
|
||||||
)),
|
|
||||||
]),
|
|
||||||
loaded_fonts: HashSet::new(),
|
loaded_fonts: HashSet::new(),
|
||||||
version: Version::default(),
|
version: Version::default(),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -438,6 +438,8 @@ impl editor::Editor for Editor {
|
||||||
cosmic_text::Action::Scroll { lines },
|
cosmic_text::Action::Scroll { lines },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
// Do nothing, these are for user convenience only
|
||||||
|
Action::Focus | Action::Blur => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.0 = Some(Arc::new(internal));
|
self.0 = Some(Arc::new(internal));
|
||||||
|
|
|
||||||
|
|
@ -421,6 +421,45 @@ where
|
||||||
shell.capture_event();
|
shell.capture_event();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Event::Touch(touch::Event::FingerPressed { position, .. }) => {
|
||||||
|
// if layout.bounds().contains(*position) {
|
||||||
|
// println!("Hey");
|
||||||
|
// let state = tree
|
||||||
|
// .state
|
||||||
|
// .downcast_mut::<State<Link, Renderer::Paragraph>>();
|
||||||
|
//
|
||||||
|
// self.hovered_link =
|
||||||
|
// state.paragraph.hit_span(*position).and_then(|span| {
|
||||||
|
// if self
|
||||||
|
// .spans
|
||||||
|
// .as_ref()
|
||||||
|
// .as_ref()
|
||||||
|
// .get(span)?
|
||||||
|
// .link
|
||||||
|
// .is_some()
|
||||||
|
// {
|
||||||
|
// Some(span)
|
||||||
|
// } else {
|
||||||
|
// None
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// state.span_pressed = self.hovered_link;
|
||||||
|
// shell.capture_event();
|
||||||
|
// if let Some(span) = self.hovered_link {
|
||||||
|
// if let Some(link) = self
|
||||||
|
// .spans
|
||||||
|
// .as_ref()
|
||||||
|
// .as_ref()
|
||||||
|
// .get(span)
|
||||||
|
// .and_then(|span| span.link.clone())
|
||||||
|
// {
|
||||||
|
// shell.publish(on_link_clicked(link));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// state.span_pressed = None;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
||||||
let state = tree
|
let state = tree
|
||||||
.state
|
.state
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ use crate::core::text::editor::{Cursor, Editor as _};
|
||||||
use crate::core::text::highlighter::{self, Highlighter};
|
use crate::core::text::highlighter::{self, Highlighter};
|
||||||
use crate::core::text::{self, LineHeight, Text, Wrapping};
|
use crate::core::text::{self, LineHeight, Text, Wrapping};
|
||||||
use crate::core::time::{Duration, Instant};
|
use crate::core::time::{Duration, Instant};
|
||||||
|
use crate::core::touch;
|
||||||
use crate::core::widget::operation;
|
use crate::core::widget::operation;
|
||||||
use crate::core::widget::{self, Widget};
|
use crate::core::widget::{self, Widget};
|
||||||
use crate::core::window;
|
use crate::core::window;
|
||||||
|
|
@ -719,6 +720,10 @@ where
|
||||||
mouse::click::Kind::Triple => Action::SelectLine,
|
mouse::click::Kind::Triple => Action::SelectLine,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if !state.is_focused() {
|
||||||
|
shell.publish(on_edit(Action::Focus));
|
||||||
|
}
|
||||||
|
|
||||||
state.focus = Some(Focus::now());
|
state.focus = Some(Focus::now());
|
||||||
state.last_click = Some(click);
|
state.last_click = Some(click);
|
||||||
state.drag_click = Some(click.kind());
|
state.drag_click = Some(click.kind());
|
||||||
|
|
@ -789,6 +794,7 @@ where
|
||||||
Binding::Unfocus => {
|
Binding::Unfocus => {
|
||||||
state.focus = None;
|
state.focus = None;
|
||||||
state.drag_click = None;
|
state.drag_click = None;
|
||||||
|
shell.publish(on_edit(Action::Blur));
|
||||||
}
|
}
|
||||||
Binding::Copy => {
|
Binding::Copy => {
|
||||||
if let Some(selection) = content.selection() {
|
if let Some(selection) = content.selection() {
|
||||||
|
|
@ -1295,6 +1301,37 @@ impl<Message> Update<Message> {
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
|
Event::Touch(event) => match event {
|
||||||
|
touch::Event::FingerPressed { .. } => {
|
||||||
|
if let Some(cursor_position) = cursor.position_in(bounds) {
|
||||||
|
let cursor_position = cursor_position
|
||||||
|
- Vector::new(padding.top, padding.left);
|
||||||
|
|
||||||
|
let click = mouse::Click::new(
|
||||||
|
cursor_position,
|
||||||
|
mouse::Button::Left,
|
||||||
|
state.last_click,
|
||||||
|
);
|
||||||
|
|
||||||
|
Some(Update::Click(click))
|
||||||
|
} else if state.focus.is_some() {
|
||||||
|
binding(Binding::Unfocus)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
touch::Event::FingerLifted { .. }
|
||||||
|
| touch::Event::FingerLost { .. } => Some(Update::Release),
|
||||||
|
touch::Event::FingerMoved { .. } => match state.drag_click {
|
||||||
|
Some(mouse::click::Kind::Single) => {
|
||||||
|
let cursor_position = cursor.position_in(bounds)?
|
||||||
|
- Vector::new(padding.top, padding.left);
|
||||||
|
|
||||||
|
Some(Update::Drag(cursor_position))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
},
|
||||||
Event::Keyboard(keyboard::Event::KeyPressed {
|
Event::Keyboard(keyboard::Event::KeyPressed {
|
||||||
key,
|
key,
|
||||||
modifiers,
|
modifiers,
|
||||||
|
|
|
||||||
|
|
@ -203,13 +203,19 @@ pub fn window_event(
|
||||||
WindowEvent::KeyboardInput { is_synthetic, .. } if is_synthetic => None,
|
WindowEvent::KeyboardInput { is_synthetic, .. } if is_synthetic => None,
|
||||||
WindowEvent::KeyboardInput { event, .. } => Some(Event::Keyboard({
|
WindowEvent::KeyboardInput { event, .. } => Some(Event::Keyboard({
|
||||||
let key = {
|
let key = {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(any(
|
||||||
|
target_arch = "wasm32",
|
||||||
|
target_os = "android",
|
||||||
|
)))]
|
||||||
{
|
{
|
||||||
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
|
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
|
||||||
event.key_without_modifiers()
|
event.key_without_modifiers()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(any(
|
||||||
|
target_arch = "wasm32",
|
||||||
|
target_os = "android",
|
||||||
|
))]
|
||||||
{
|
{
|
||||||
// TODO: Fix inconsistent API on Wasm
|
// TODO: Fix inconsistent API on Wasm
|
||||||
event.logical_key.clone()
|
event.logical_key.clone()
|
||||||
|
|
@ -217,7 +223,10 @@ pub fn window_event(
|
||||||
};
|
};
|
||||||
|
|
||||||
let text = {
|
let text = {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(any(
|
||||||
|
target_arch = "wasm32",
|
||||||
|
target_os = "android",
|
||||||
|
)))]
|
||||||
{
|
{
|
||||||
use crate::core::SmolStr;
|
use crate::core::SmolStr;
|
||||||
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
|
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
|
||||||
|
|
@ -225,6 +234,18 @@ pub fn window_event(
|
||||||
event.text_with_all_modifiers().map(SmolStr::new)
|
event.text_with_all_modifiers().map(SmolStr::new)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "android")]
|
||||||
|
{
|
||||||
|
if event.text.is_some() {
|
||||||
|
event.text
|
||||||
|
} else {
|
||||||
|
match &key {
|
||||||
|
winit::keyboard::Key::Character(c) => Some(c.clone()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
{
|
{
|
||||||
// TODO: Fix inconsistent API on Wasm
|
// TODO: Fix inconsistent API on Wasm
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue