Scrolling text for user information
⚓ Rust 📅 2026-04-25 👤 surdeus 👁️ 1Hi there!
I'm newbee in rust. I'm coding an application, and i would like to display differents messages for user on a line which has the width of the window. The different messages have different widths, and i would like these messages to scroll from the right to the left of the window, character by character. One message in a time. I know this would be jerky, and that's what i'm looking for. I gave a width to the app window in main like iced::application().window_size(). I should add spaces to fit the width of the line and then make it scroll. i've made a test application and my messages scroll well, but doesn't fit the line (the container). Here is my code
use iced::time::every;
use iced::{Center, Element, Subscription, Length::Fill, Background, Color, Theme, Border};
use std::time::Duration;
use iced::widget::{container, Button, Column, Container, Text};
fn main() -> iced::Result {
iced::application(App::new, App::update, App::view)
.subscription(App::subscription)
.title("texte qui défile")
.window_size((600.0, 400.0))
.run()
}
struct App {
status_bar: StatusBar,
}
pub struct StatusBar {
pub message_utilisateur: String,
}
#[derive(Debug, Clone)]
pub enum Message {
Tick,
SetState(AppState),
}
#[derive(Debug, Clone)]
pub enum AppState {
PremierMessage,
DeuxiemeMessage,
TroisiemeMessage,
}
impl StatusBar {
pub fn new(message: impl Into) -> Self {
Self {
message_utilisateur: message.into(),
}
}
pub fn tick(&mut self) {
self.message_utilisateur = texte_kidefil(self.message_utilisateur.clone());
}
fn update(&mut self, message: AppState) {
match message {
AppState::PremierMessage => {
self.message_utilisateur = "Premier message ".to_string();
}
AppState::DeuxiemeMessage => {
self.message_utilisateur = "Deuxième message ".to_string();
}
AppState::TroisiemeMessage => {
self.message_utilisateur = "Troisième message ".to_string();
}
}
}
pub fn view(&self) -> Element<'_, Message> {
Container::new(Text::new(&self.message_utilisateur))
.width(Fill)
.padding(8)
.style(container_style)
.into()
}
}
impl App {
fn new() -> Self {
Self {
status_bar: StatusBar::new("texte de départ "),
}
}
fn update(&mut self, message: Message) {
match message {
Message::Tick => {
self.status_bar.tick();
}
Message::SetState(state) => {
self.status_bar.update(state);
}
}
}
fn view(&self) -> Element<'_, Message> {
Column::new()
.push(Text::new("na oir sa meme "))
.push(Button::new("premier message ")
.on_press(Message::SetState(AppState::PremierMessage)))
.push(Button::new("deuxième message ")
.on_press(Message::SetState(AppState::DeuxiemeMessage)))
.push(Button::new("troisième message ")
.on_press(Message::SetState(AppState::TroisiemeMessage)))
.push(self.status_bar.view())
.width(Fill)
.align_x(Center)
.into()
}
fn subscription(&self) -> Subscription<Message> {
every(Duration::from_millis(200))
.map(|_| Message::Tick)
}
}
pub fn texte_kidefil(kidefilpa: String) -> String {
//println!("AVANT: {}", kidefilpa);
let mut c = kidefilpa.chars();
let resultat = match c.next() {
Some(premier) => {
let reste: String = c.collect();
format!("{}{}", reste, premier)
}
None => kidefilpa,
};
//println!("APRES: {}", resultat);
resultat
}
pub fn container_style(_: &Theme) -> container::Style {
let base = container::Style {
background: Some(Background::Color(Color::from_rgb(0.34, 0.428, 0.188))), // DARK_OLIVE_GREEN
text_color: Option::from(Some(Color::from_rgb(0.872, 0.66, 0.128)).unwrap()), // GOLDEN_ROD
border: Border {
width: 1.0,
color: Color::from_rgb(0.34, 0.428, 0.188),
..Default::default()
},
..Default::default()
};
base
}
The container_style is to show the size of it.
Could it be easy or not, and how ?
1 post - 1 participant
🏷️ Rust_feed