<Rust>egui学习之小部件(四):如何在窗口中添加滚动条Scroll部件?

前言
本专栏是关于Rust的GUI库egui的部件讲解及应用实例分析,主要讲解egui的源代码、部件属性、如何应用。

环境配置
系统:windows
平台:visual studio code
语言:rust
库:egui、eframe

概述
本文是本专栏的第四篇博文,主要讲述滑动条部件的使用。

事实上,类似于iced,egui都提供了示例程序,本专栏的博文都是建立在官方示例程序以及源代码的基础上,进行的实例讲解。
即,本专栏的文章并非只是简单的翻译egui的官方示例与文档,而是针对于官方代码进行的实际使用,会在官方的代码上进行修改,包括解决一些问题。

系列博客链接:
1、<Rust>egui学习之小部件(一):如何在窗口及部件显示中文字符?
2、<Rust>egui学习之小部件(二):如何在egui窗口中添加按钮button以及标签label部件?
3、<Rust>egui学习之小部件(三):如何为窗口UI元件设置布局(间隔、水平、垂直排列)?

部件属性

有时候我们会需要在窗口设置滚动条,或者滑动条这样的部件,来实现滚动操作。在egui中,如果要添加滚动条,则使用ScrollArea部件,它表示的是创造一块区域,这个区域内显示滚动条操作,有两个方向,一个是水平滚动,一个是垂直滚动,可以单独设置,也可以都选择。
ScrollArea的官方定义:

#[derive(Clone, Debug)]     
#[must_use = "You should call .show()"]
pub struct ScrollArea {/// Do we have horizontal/vertical scrolling enabled?scroll_enabled: Vec2b,auto_shrink: Vec2b,max_size: Vec2,min_scrolled_size: Vec2,scroll_bar_visibility: ScrollBarVisibility,id_source: Option<Id>,offset_x: Option<f32>,offset_y: Option<f32>,/// If false, we ignore scroll events.scrolling_enabled: bool,drag_to_scroll: bool,/// If true for vertical or horizontal the scroll wheel will stick to the/// end position until user manually changes position. It will become true/// again once scroll handle makes contact with end.stick_to_end: Vec2b,/// If false, `scroll_to_*` functions will not be animatedanimated: bool,
}
添加滚动条区域
egui::ScrollArea::both().enable_scrolling(true)  .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible).show_rows(ui,row_height,total_rows,|ui,rowrange|{ui.horizontal(|ui|{for i in 0..8{ui.label(format!("label{}",i));}});ui.vertical(|ui|{for i in 0..10{ui.label(format!("label{}",i));}})});

在这里插入图片描述

如上图,滚动条在窗口中作为部件显示,其显示区域可以通过布局调整。正常来说,我们只需要滚动条的拖动功能,即无需其状态反馈。
但如果需要也可以设置,ScrollArea有State属性,用于接受对ScrollArea操作时的状态反馈:

let sc1=egui::ScrollArea::both().enable_scrolling(true)  .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::VisibleWhenNeeded).max_height(200.0).show_rows(ui,row_height,total_rows,|ui,rowrange|{ui.set_height(200.0);ui.set_width(100.0);ui.horizontal(|ui|{for i in 0..8{ui.label(format!("label{}",i));}});ui.vertical(|ui|{for i in 0..10{ui.label(format!("label{}",i));}})}); ui.label(format!("scroll area:{}",sc1.state.offset.x));ui.label(format!("scroll area:{}",sc1.state.offset.y)); 

如上,我们添加两个标签,用于显示滚动条滑动时滑块的移动量:
在这里插入图片描述
State参数如下:

#[derive(Clone, Copy, Debug)]          
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct State {/// Positive offset means scrolling down/rightpub offset: Vec2,/// If set, quickly but smoothly scroll to this target offset.offset_target: [Option<ScrollTarget>; 2],/// Were the scroll bars visible last frame?show_scroll: Vec2b,/// The content were to large to fit large frame.content_is_too_large: Vec2b,/// Did the user interact (hover or drag) the scroll bars last frame?scroll_bar_interaction: Vec2b,/// Momentum, used for kinetic scrolling#[cfg_attr(feature = "serde", serde(skip))]vel: Vec2,/// Mouse offset relative to the top of the handle when started moving the handle.scroll_start_offset_from_top_left: [Option<f32>; 2],/// Is the scroll sticky. This is true while scroll handle is in the end position/// and remains that way until the user moves the scroll_handle. Once unstuck (false)/// it remains false until the scroll touches the end position, which reenables stickiness.scroll_stuck_to_end: Vec2b,/// Area that can be dragged. This is the size of the content from the last frame.interact_rect: Option<Rect>,
}

如果要设置其样式,那么我们需要设置其Style。
但ScrollArea并没有单独的Style设置,不过可以通用的Style进行设置,可以修改字体尺寸和字体样式:

#[derive(Clone, Debug, PartialEq)]     
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Style {/// If set this will change the default [`TextStyle`] for all widgets.////// On most widgets you can also set an explicit text style,/// which will take precedence over this.pub override_text_style: Option<TextStyle>,/// If set this will change the font family and size for all widgets.////// On most widgets you can also set an explicit text style,/// which will take precedence over this.pub override_font_id: Option<FontId>,/// The [`FontFamily`] and size you want to use for a specific [`TextStyle`].////// The most convenient way to look something up in this is to use [`TextStyle::resolve`].////// If you would like to overwrite app `text_styles`////// ```/// # let mut ctx = egui::Context::default();/// use egui::FontFamily::Proportional;/// use egui::FontId;/// use egui::TextStyle::*;////// // Get current context style/// let mut style = (*ctx.style()).clone();////// // Redefine text_styles/// style.text_styles = [///   (Heading, FontId::new(30.0, Proportional)),///   (Name("Heading2".into()), FontId::new(25.0, Proportional)),///   (Name("Context".into()), FontId::new(23.0, Proportional)),///   (Body, FontId::new(18.0, Proportional)),///   (Monospace, FontId::new(14.0, Proportional)),///   (Button, FontId::new(14.0, Proportional)),///   (Small, FontId::new(10.0, Proportional)),/// ].into();////// // Mutate global style with above changes/// ctx.set_style(style);/// ```pub text_styles: BTreeMap<TextStyle, FontId>,/// The style to use for [`DragValue`] text.pub drag_value_text_style: TextStyle,/// How to format numbers as strings, e.g. in a [`crate::DragValue`].////// You can override this to e.g. add thousands separators.#[cfg_attr(feature = "serde", serde(skip))]pub number_formatter: NumberFormatter,/// If set, labels, buttons, etc. will use this to determine whether to wrap the text at the/// right edge of the [`Ui`] they are in. By default, this is `None`.////// **Note**: this API is deprecated, use `wrap_mode` instead.////// * `None`: use `wrap_mode` instead/// * `Some(true)`: wrap mode defaults to [`crate::TextWrapMode::Wrap`]/// * `Some(false)`: wrap mode defaults to [`crate::TextWrapMode::Extend`]#[deprecated = "Use wrap_mode instead"]pub wrap: Option<bool>,/// If set, labels, buttons, etc. will use this to determine whether to wrap or truncate the/// text at the right edge of the [`Ui`] they are in, or to extend it. By default, this is/// `None`.////// * `None`: follow layout (with may wrap)/// * `Some(mode)`: use the specified mode as defaultpub wrap_mode: Option<crate::TextWrapMode>,/// Sizes and distances between widgetspub spacing: Spacing,/// How and when interaction happens.pub interaction: Interaction,/// Colors etc.pub visuals: Visuals,/// How many seconds a typical animation should last.pub animation_time: f32,/// Options to help debug why egui behaves strangely.////// Only available in debug builds.#[cfg(debug_assertions)]pub debug: DebugOptions,/// Show tooltips explaining [`DragValue`]:s etc when hovered.////// This only affects a few egui widgets.pub explanation_tooltips: bool,/// Show the URL of hyperlinks in a tooltip when hovered.pub url_in_tooltip: bool,/// If true and scrolling is enabled for only one direction, allow horizontal scrolling without pressing shiftpub always_scroll_the_only_direction: bool,
}

但好像没有修改背景颜色、修改滑块样式这些选项,可自定义的还是比较少。但是如果只想要修改文字样式,那么可以针对label部件单独设置,可以适应Richtext来创建标签文本,那么就可以设置颜色、背景色、下划线等多种自定义样式:

ui.label(RichText::new(format!("标签{}",i)).color(Color32::from_rgb(0, 0, 0)).underline().strikethrough().background_color(Color32::from_rgb(255, 0, 255)));

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/413517.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

今日算法:蓝桥杯基础题之“切面条”

你好同学&#xff0c;我是沐爸&#xff0c;欢迎点赞、收藏、评论和关注&#xff01;个人知乎 从今天开始&#xff0c;一起了解算法&#xff0c;每日一题&#xff0c;从 JavScript 的技术角度进行解答&#xff0c;如果你对算法也感兴趣&#xff0c;请多多关注哦。 问题描述 一…

【深度学习与NLP】——深度卷积神经网络AlexNet

目录 一、卷积神经网络的发展历程 二、简要介绍 三、代码实现 四、缺点和过时的地方 一、卷积神经网络的发展历程 早期理论基础阶段&#xff08;20 世纪 60 年代 - 80 年代&#xff09;&#xff1a; 1968 年&#xff0c;Hubel 和 Wiesel 通过对猫视觉神经的研究&#xff0…

Hibernate 批量插入速度慢的原因和解决方法

由于业务需要一次性连续写入超过10k条以上的新数据&#xff0c;当对象超过10个成员变量以后&#xff0c;整个写入过程居然需要长达35秒&#xff0c;这个速度是不能接受的&#xff0c;故此研究了一下怎么开启Hibernate批量写入的功能。 我这边使用的是Hibernate 5.6.15 在网上…

Python 从入门到实战3(列表的简单操作)

我们的目标是&#xff1a;通过这一套资料学习下来&#xff0c;通过熟练掌握python基础&#xff0c;然后结合经典实例、实践相结合&#xff0c;使我们完全掌握python&#xff0c;并做到独立完成项目开发的能力。 上篇文章我们通过python小栗子来学习python基础知识语法&#xff…

C语言中的“#”和“##”

目录 开头1.什么是#?2.什么是##?3.#和##的实际应用输出变量的名字把两个符号连接成一个符号输出根据变量的表达式…… 下一篇博客要说的东西 开头 大家好&#xff0c;我叫这是我58。在今天&#xff0c;我们要学一下关于C语言中的#和##的一些知识。 1.什么是#? #&#xff0…

《黑神话:悟空》:30%抽成真相

《黑神话&#xff1a;悟空》自建服务器出售&#xff1f;揭秘游戏界的30%抽成真相&#xff01; 近年来&#xff0c;随着游戏行业的迅猛发展&#xff0c;游戏开发商与发行平台之间的利益分配问题逐渐成为业界关注的焦点。其中&#xff0c;《黑神话&#xff1a;悟空》作为一款备受…

JS基础之【基本数据类型与类型间的隐式显示转换】

&#x1f680; 个人简介&#xff1a;某大型国企高级前端开发工程师&#xff0c;7年研发经验&#xff0c;信息系统项目管理师、CSDN优质创作者、阿里云专家博主&#xff0c;华为云云享专家&#xff0c;分享前端后端相关技术与工作常见问题~ &#x1f49f; 作 者&#xff1a;码…

streamlit+wordcloud使用pyinstaller打包遇到的一些坑

说明 相比常规的python程序打包&#xff0c;streamlit应用打包需要额外加一层壳&#xff0c;常规app.py应用运行直接使用 python app.py就可以运行程序了&#xff0c;但streamlit应用是需要通过streamlit命令来运行 streamlit app.py所以使用常规的pyinstaller app.py打包是…

云同步的使用

云同步技术是一种在多个设备或系统之间保持数据一致性的技术&#xff0c;它通常依赖于云存储服务来实现。在Java中&#xff0c;实现云同步功能通常需要与云服务提供商的API进行交互&#xff0c;如Amazon S3、Google Cloud Storage、Microsoft Azure Blob Storage等。 以下是一个…

秋风送爽,夏意未央|VELO Prevail Revo坐垫,一骑绿动起来吧~

夏末秋初&#xff0c;当第一片落叶缓缓飘落&#xff0c;是时候骑上你的自行车&#xff0c;迎接新的季节啦。带上维乐Prevail Revo坐垫&#xff0c;因为它独树一帜地采用EVA与回收咖啡渣精制而成的轻量发泡提升了减震性能&#xff0c;可以让你的每一次骑行都充满意义。    “…

虚幻引擎(Unreal Engine)技术使得《黑神话悟空传》大火,现在重视C++的开始吃香了,JAVA,Go,Unity都不能和C++相媲美!

虚幻引擎&#xff08;Unreal Engine&#xff09;火了黑神话游戏。 往后&#xff0c;会有大批量的公司开始模仿这个赛道&#xff01; C 的虚拟引擎技术通常指的是使用 C 语言开发的游戏引擎&#xff0c;如虚幻引擎&#xff08;Unreal Engine&#xff09;等。以下是对 C 虚拟引…

【virtuoso】INV 原理图+前仿真 + 版图 + 后仿真

采用SMIC工艺&#xff0c;不同工艺版图窗口可能有差异 1. 原理图&前仿真 1.1 绘制原理图 PMOS: NMOS宽长比2&#xff1a;1 PMOS开启导通电阻大一点&#xff0c;这样设置&#xff0c;可以使得阈值电压是VDD/2 按 i&#xff0c;可以插入器件按p&#xff0c;可以放置端口 1.2…

【机器学习】聚类算法的基本概念和实例代码以及局部度量学习的概念和实例代码

引言 聚类算法在许多领域都有广泛的应用&#xff0c;例如数据挖掘、生物信息学、图像处理等。 文章目录 引言一、聚类算法1.1 K-Means算法1.2 DBSCAN算法1.3 层次聚类&#xff08;Hierarchical Clustering&#xff09;算法1.4 高斯混合模型&#xff08;Gaussian Mixture Model&…

Web自动化测试实战--博客系统

&#x1f3a5; 个人主页&#xff1a;Dikz12&#x1f525;个人专栏&#xff1a;测试&#x1f4d5;格言&#xff1a;吾愚多不敏&#xff0c;而愿加学欢迎大家&#x1f44d;点赞✍评论⭐收藏 目录 1.项目效果展示 2.编写web测试用例 3.自动化测试脚本开发 3.1创建空项目 引…

Web-gpt

AJAX AJAX&#xff08;Asynchronous JavaScript and XML&#xff0c;异步JavaScript和XML&#xff09;是一种用于创建动态网页应用的技术。它允许网页在不重新加载整个页面的情况下&#xff0c;异步地从服务器请求数据&#xff0c;并将这些数据更新到网页上。这提高了用户体验…

大语言模型-GLM-General Language Model Pretraining

一、背景信息&#xff1a; GLM是2020-2021年由智谱AI研究并发布的预训练语言模型。 GLM是一种基于自回归空白填充的通用预训练语言模型。 GLM 通过添加二维位置编码和允许任意顺序预测空白区域&#xff0c;改进了空白填充预训练&#xff0c;在NLU任务上超越了 BERT 和 T5。 GL…

12 对话模型微调2

1 P-Tuning P-Tuning 是在 Prompt-Tuning的基础上&#xff0c;通过新增 LSTM 或 MLP 编码模块来加速模型的收敛&#xff1b; 之前的实验也看到了使用prompt训练速度很慢&#xff0c;那么P-Tuning呢 参数占比&#xff1a; trainable params: 5,267,456 || all params: 1,308,37…

Llamaindex RAG实践

加入xtunert文档作为提示词 让大模型理解xtuner

Python 算法交易实验85 QTV200日常推进-钳制指标与交易量

说明 继续保持思考与尝试 最近挺有意思的&#xff0c;碰到很多技术上的问题&#xff0c;其解决方案都类似“阴阳两仪”的概念。 "阴阳两仪"是中国古代哲学中的一个重要概念&#xff0c;源自《易经》&#xff08;又称《周易》&#xff09;。它是对宇宙间最基本对立统一…

Java数据结构(七)——优先级队列与PriorityQueue

文章目录 优先级队列与PriorityQueue堆基本概念和性质建堆堆的插入堆的删除堆的应用 PriorityQueuePriorityQueue的构造方法PriorityQueue的常用方法PriorityQueue的模拟实现 经典TopK问题 优先级队列与PriorityQueue 优先级队列是一种特殊类型的队列&#xff0c;其中元素按照…