首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Rust GUI开发入门】编写一个本地音乐播放器(10. 拼装UI组件)

【Rust GUI开发入门】编写一个本地音乐播放器(10. 拼装UI组件)

原创
作者头像
用户11855011
发布2025-09-30 22:46:30
发布2025-09-30 22:46:30
800
举报

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

将前文写的零散的UI组件(歌曲列表/播放控制面板/歌词面板/设置面板)拼装起来,组成最终的音乐播放器UI,用TabWidget分成多标签页,每一个标签页分担不同的功能:

player.png
player.png
代码语言:slint
复制
export component MainWindow inherits Window {
    preferred-width: 850px;
    preferred-height: 500px;
    min-width: 850px;
    min-height: 500px;
    title: "Zeedle";
    icon: @image-url("cover.svg");
    callback toggle_play();
    callback play(SongInfo, TriggerSource);
    callback play_next();
    callback play_prev();
    callback change_progress(float);
    callback switch_mode(PlayMode);
    callback refresh_song_list(string);
    callback sort_song_list(SortKey, bool);
    callback set_lang(string);
    pure callback format_duration(float) -> string;
    public function set_light_theme(yes: bool) {
        UIState.light_ui = yes;
        if (yes) {
            Palette.color-scheme = ColorScheme.light;
        } else {
            Palette.color-scheme = ColorScheme.dark;
        }
    }
    tabs := TabWidget {
        Tab {
            title: @tr("Gallery");
            VerticalLayout {
                SongListView {
                    ascending <=> UIState.sort_ascending;
                    sort-key <=> UIState.sort_key;
                    last-sort-key <=> UIState.last_sort_key;
                    song-list <=> UIState.song_list;
                    sort-songs(key, asc) => {
                        root.sort_song_list(key, asc);
                    }
                    play-song(info, src) => {
                        root.play(info, src);
                    }
                }

                ControlPanel {
                    max-height: 80px;
                    min-height: 50px;
                    preferred-height: 60px;
                    progress <=> UIState.progress;
                    duration <=> UIState.duration;
                    paused <=> UIState.paused;
                    dragging <=> UIState.dragging;
                    play_mode <=> UIState.play_mode;
                    current_song <=> UIState.current_song;
                    album_image <=> UIState.album_image;
                    change-progress(p) => {
                        root.change_progress(p);
                        root.focus();
                    }
                    toggle-play() => {
                        root.toggle_play();
                    }
                    play-next() => {
                        root.play_next();
                    }
                    play-prev() => {
                        root.play_prev();
                    }
                    switch-mode(m) => {
                        root.switch_mode(m);
                    }
                    double-clicked() => {
                        tabs.current-index = 1; // 切换到歌词页
                    }
                    format-duration(d) => {
                        return root.format_duration(d);
                    }
                }
            }
        }

        Tab {
            title: @tr("Lyrics");
            Rectangle {
                width: 100%;
                height: 100%;
                z: -5;
                TouchArea {
                    double-clicked => {
                        tabs.current-index = 0; // 切换到歌曲列表页
                    }
                }
            }

            LyricsPanel {
                width: 100%;
                height: 100%;
                album_image <=> UIState.album_image;
                current_song <=> UIState.current_song;
                lyrics <=> UIState.lyrics;
                progress <=> UIState.progress;
                lyric_viewport_y <=> UIState.lyric_viewport_y;
            }
        }

        Tab {
            title: @tr("Settings");
            SettingsPanel {
                width: 100%;
                height: 100%;
                song_dir <=> UIState.song_dir;
                lang <=> UIState.lang;
                light_ui <=> UIState.light_ui;
                refresh_song_list(p) => {
                    root.refresh_song_list(p);
                }
                set_lang(l) => {
                    root.set_lang(l);
                }
                set_light_theme(yes) => {
                    root.set_light_theme(yes);
                }
            }
        }

        Tab {
            title: @tr("About");
            Text {
                width: 100%;
                height: 100%;
                font-size: 16px;
                text: UIState.about_info;
                horizontal-alignment: center;
                vertical-alignment: center;
            }
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档