如何在 Iced 0.12 中设置按钮样式?

问题描述 投票:0回答:2

我是 Rust 语言新手,我想设计我的按钮样式,以便在进程仍在运行时可以将其禁用。

我用的是冰镇0.12.1。

这是我的代码片段:

#[derive(Debug, Clone, Copy)]
pub struct CustomButtonStyle;

impl StyleSheet for CustomButtonStyle {
    type Style = ();

    fn active(&self, _: &Self::Style) -> Appearance {
        Appearance {
            background: Some(Background::Color(Color::from_rgb(0.2, 0.6, 0.2))),
            text_color: Color::WHITE,
            border: Border::default(),
            shadow: Shadow::default(),
            shadow_offset: Vector::default(),
        }
    }

    fn hovered(&self, _: &Self::Style) -> Appearance {
        Appearance {
            background: Some(Background::Color(Color::from_rgb(0.3, 0.7, 0.3))),
            text_color: Color::WHITE,
            border: Border::default(),
            shadow: Shadow::default(),
            shadow_offset: Vector::default(),
        }
    }

    fn disabled(&self, _: &Self::Style) -> Appearance {
        Appearance {
            background: Some(Background::Color(Color::from_rgb(0.5, 0.5, 0.5))),
            text_color: Color::from_rgb(0.7, 0.7, 0.7),
            border: Border::default(),
            shadow: Shadow::default(),
            shadow_offset: Vector::default(),
        }
    }
}

...
                    button("Process")
                      .on_press(Message::Process)
                      .padding(Padding::from(5))
                      .style(CustomButtonStyle) //ERROR
...

错误说:

不满足特质界限

iced::theme::Button: From<CustomButtonStyle>
需要
CustomButtonStyle
才能实施
Into<iced::theme::Button>

完整错误:

error[E0277]: the trait bound `iced::theme::Button: From<CustomButtonStyle>` is not satisfied
   --> src\test_rust.rs:332:32
    |
332 |                         .style(CustomButtonStyle)
    |                          ----- ^^^^^^^^^^^^^^^^^ the trait `From<CustomButtonStyle>` is not implemented for `iced::theme::Button`, which is required by `CustomButtonStyle: Into<_>`
    |                          |
    |                          required by a bound introduced by this call
    |
    = note: required for `CustomButtonStyle` to implement `Into<iced::theme::Button>`
note: required by a bound in `iced::widget::Button::<'a, Message, Theme, Renderer>::style`
   --> C:\Users\\.cargo\registry\src\index.crates.io-6f17d22bba15001f\iced_widget-0.12.3\src\button.rs:127:40
    |
127 |     pub fn style(mut self, style: impl Into<Theme::Style>) -> Self {
    |                                        ^^^^^^^^^^^^^^^^^^ required by this bound in `Button::<'a, Message, Theme, Renderer>::style`

我搜索并找到了这个:https://github.com/iced-rs/iced/issues/882

所以我尝试了:

.style(iced::theme::Button::CustomButtonStyle)

并收到此错误:

在当前范围内未找到名为

CustomButtonStyle
的枚举
iced::theme::Button
的变体或关联项 在
Button

中找不到变体或相关项目

完整错误:

error[E0271]: type mismatch resolving `<CustomButtonStyle as StyleSheet>::Style == Theme`
   --> src\frontend_desktop.rs:331:60
    |
331 |                         .style(iced::theme::Button::custom(CustomButtonStyle))
    |                                --------------------------- ^^^^^^^^^^^^^^^^^ type mismatch resolving `<CustomButtonStyle as StyleSheet>::Style == Theme`
    |                                |
    |                                required by a bound introduced by this call
    |
note: expected this to be `Theme`
   --> src\test_rust.rs:15:18
    |
15  |     type Style = ();
    |                  ^^
note: required by a bound in `iced::theme::Button::custom`
   --> C:\Users\\.cargo\registry\src\index.crates.io-6f17d22bba15001f\iced_style-0.12.1\src\theme.rs:311:46
    |
310 |     pub fn custom(
    |            ------ required by a bound in this associated function
311 |         style_sheet: impl button::StyleSheet<Style = Theme> + 'static,
    |                                              ^^^^^^^^^^^^^ required by this bound in `Button::custom`

我也尝试过改变

type Style = iced::theme::Button::Custom;

出现错误告诉我:

  • 板条箱::frontend_desktop::主题::自定义
  • 冰::主题::自定义
  • iced_style::主题::自定义

然后我跟着:

type Style = iced::theme::Custom;

“type Style”行和“.style(___)”行都出现错误。

行“type Style”的完整错误:

error[E0277]: the trait bound `iced::theme::Custom: std::default::Default` is not satisfied
  --> src\test_rust.rs:14:18
   |
14 |     type Style = iced::theme::Custom;
   |                  ^^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `iced::theme::Custom`
   |
note: required by a bound in `iced::widget::button::StyleSheet::Style`
  --> C:\Users\\.cargo\registry\src\index.crates.io-6f17d22bba15001f\iced_style-0.12.1\src\button.rs:34:17
   |
34 |     type Style: Default;
   |                 ^^^^^^^ required by this bound in `StyleSheet::Style`
rust rust-iced
2个回答
1
投票

我相信对于冰 0.12,您需要执行以下操作:

  • 设置
    Style = iced::Theme
  • 致电
    .style(iced::theme::Button::custom(CustomButtonStyle))

您收到错误是因为您的按钮使用默认主题,因此您需要提供默认主题的样式。值得庆幸的是,他们提供了一个自定义变体,让您可以提供自己的样式 - 但您需要在该变体中正确包装您的自定义样式。

请注意,iced 0.13 似乎完全改变了 .style 的工作方式。


0
投票

怎么样:

pub fn custom(_: &Theme, status: Status) -> Style {
    match status {
        Status::Active | Status::Pressed => Style {
            background: Some(Background::Color(Color::from_rgb(0.2, 0.6, 0.2))),
            text_color: Color::WHITE,
            border: Border::default(),
            shadow: Shadow::default(),
        },
        Status::Hovered => Style {
            background: Some(Background::Color(Color::from_rgb(0.3, 0.7, 0.3))),
            text_color: Color::WHITE,
            border: Border::default(),
            shadow: Shadow::default(),
        },
        Status::Disabled => Style {
            background: Some(Background::Color(Color::from_rgb(0.5, 0.5, 0.5))),
            text_color: Color::from_rgb(0.7, 0.7, 0.7),
            border: Border::default(),
            shadow: Shadow::default(),
        },
    }
}

然后:

button("Process")
 .on_press(Message::Process)
 .padding(Padding::from(5))
 .style(custom)
© www.soinside.com 2019 - 2024. All rights reserved.