【Rust GUI开发入门】编写一个本地音乐播放器(7. 制作歌词显示面板)

本系列教程对应的代码已开源在 Github zeedle

目的是要制作一个这样的面板显示歌词:

水平布局:

  • 左边30%显示专辑封面
  • 右边70%显示歌词

歌词仍然使用ListView来构建,跟前文的歌曲列表一样,代码如下:

slint 复制代码
export component LyricsPanel inherits Window {
    in property <image> album_image;
    in property <SongInfo> current_song;
    in property <[LyricItem]> lyrics;
    in property <float> progress;
    in-out property <length> lyric_viewport_y;
    HorizontalLayout {
        width: 100%;
        height: 100%;
        VerticalLayout {
            width: 30%;
            alignment: center;
            lyric-image := Rectangle {
                x: parent.width / 2 - self.width / 2;
                width: 180px;
                height: self.width;
                clip: true;
                border-radius: 8px;
                drop-shadow-blur: 8px;
                Image {
                    width: 100%;
                    height: 100%;
                    source: album_image;
                }
            }

            Text {
                width: 100%;
                height: 25px;
                font-size: 13px;
                x: lyric-image.x;
                vertical-alignment: bottom;
                text: @tr("Artist: {}", current_song.singer);
                overflow: elide;
            }

            Text {
                width: 100%;
                height: 25px;
                font-size: 13px;
                x: lyric-image.x;
                vertical-alignment: bottom;
                text: @tr("Title: {}", current_song.song_name);
                overflow: elide;
            }
        }

        VerticalLayout {
            width: 70%;
            alignment: center;
            ListView {
                height: 100%;
                width: 100%;
                mouse-drag-pan-enabled: false;
                viewport-y <=> lyric_viewport_y;
                for item in lyrics: LyricLine {
                    content: item.text;
                    playing: (progress >= item.time) && (progress < item.time + item.duration);
                }
            }
        }
    }
}

重点在于从文件中解析歌词,将在下文介绍。

相关推荐
光影少年26 分钟前
vite+rust生态链工具链
开发语言·前端·后端·rust·前端框架
techdashen1 小时前
服务不停,升级照常:Cloudflare 是怎么做到零中断重启的
开发语言·rust
Rust研习社1 小时前
Reqwest 兼顾简洁与高性能的现代 HTTP 客户端
开发语言·网络·后端·http·rust
Rust研习社18 小时前
添加依赖库时的 features 是什么?优雅实现编译期条件编译与模块化开发
开发语言·后端·rust
Rust研习社19 小时前
Rust 条件变量(Condvar)详解:线程同步的高效方式
后端·rust·编程语言
Rust研习社19 小时前
Rust Channel 详解:线程间安全通信的利器
后端·rust·编程语言
Source.Liu1 天前
【A11】身份证号无损压缩到48位的Rust实现
rust
圆山猫1 天前
[RISCV] 用 Rust 写一个 RISC-V BootROM:从 QEMU 到真实硬件(2)
rust·risc-v
Ulyanov1 天前
《PySide6 GUI开发指南:QML核心与实践》 第二篇:QML语法精要——构建声明式UI的基础
java·开发语言·javascript·python·ui·gui·雷达电子对抗系统仿真
Rust研习社2 天前
Once、OnceCell、OnceLock:Rust 一次性初始化终极指南
后端·rust·编程语言