iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm
https://iced.rs
MIT License
24.73k stars 1.16k forks source link

It is hoped that it can normally use non-English languages such as Chinese #2542

Open lethargy123 opened 3 months ago

lethargy123 commented 3 months ago

Is your issue REALLY a bug?

Is there an existing issue for this?

Is this issue related to iced?

What happened?

I know that widgets like Button can use .shaping(text::Shaping::Advanced) for shaping text. However, if the entire project is in Chinese, it's inconvenient to use .shaping(text::Shaping::Advanced) in various widgets or wrap it in a function. Additionally, widgets like Checkbox cannot use .shaping(text::Shaping::Advanced). I hope this issue can be fixed (I suggest making .shaping(text::Shaping::Advanced) the default for text).

What is the expected behavior?

It can normally use Chinese and other non-English languages

Version

master

Operating System

Windows

Do you have any log output?

No response

alex-ds13 commented 3 months ago

Checkboxes have a .text_shaping() method exactly for this. And you can make your own helpers to not have to do that every time, like this:

mod widget_helpers {
  /// Creates a new [`Checkbox`].
  ///
  /// [`Checkbox`]: crate::Checkbox
  pub fn checkbox<'a, Message, Theme, Renderer>(
      label: impl Into<String>,
      is_checked: bool,
  ) -> Checkbox<'a, Message, Theme, Renderer>
  where
      Theme: checkbox::Catalog + 'a,
      Renderer: core::text::Renderer,
  {
      Checkbox::new(label, is_checked).text_shaping(text::Shaping::Advanced)
  }

  pub fn button<'a>(text: impl Into<Text<'a>>) -> Button<'a, Message> {
      button(text.into().shaping(text::Shaping::Advanced))
  }

  /// Creates a new [`Text`] widget with the provided content.
  pub fn text<'a, Theme, Renderer>(
      text: impl text::IntoFragment<'a>,
  ) -> Text<'a, Theme, Renderer>
  where
      Theme: text::Catalog + 'a,
      Renderer: core::text::Renderer,
  {
      Text::new(text).shaping(text::Shaping::Advanced)
  }
}

Then instead of importing the normal checkbox, button, text from iced::widget you import them from this widget_helpers (name is just an example) and use them normally like this:

use widget_helpers::*;

pub fn view(&self) -> Element<Message> {
  checkbox("Whatever you want", true).into();
}
alex-ds13 commented 3 months ago

Other widgets like the radio, toggler, pick_list also have the same method for text shaping. Text inputs I believe use advanced shaping by default and that's why they don't have it...

teknalb commented 2 months ago

That's weird if some widgets use advanced text shaping and others don't!

C-Entropy commented 1 week ago

Maybe it ispossible to auto update shaping according to the string content. There is a function is_ascii from std. If it can be done, does it have any value? If it have value, I may give it a try to make it work.