Archive / / / / / Echo.UI.Widgets.TrackSliderToolItem.vala
2008-12-02 07:11:17 UTC
previous next
/* vim: set noexpandtab tabstop=4 shiftwidth=4 nowrap textwidth=100 * * Echo Media Player * Copyright (C) 2008 Shane O'Connell * * [ The original file includes a copyright header in this location describing * the file as being released under the terms of the GNU General Public * License. It has been removed in order to display the file as part of the * archive. ] */ using Echo.Core; using Echo.Playback; public class Echo.UI.Widgets.TrackSliderToolItem : Gtk.ToolItem { private Gtk.HScale _scale; private Gtk.Label _label; private bool _timeout_stopped = true; construct { var vbox = new Gtk.VBox(false, 0); _scale = new Gtk.HScale.with_range(0.0, 100.0, 1.0); _label = new Gtk.Label(""); _scale.set_draw_value(false); _scale.set_sensitive(false); var scale_align = new Gtk.Alignment(0.5f, 1.0f, 1.0f, 1.0f); scale_align.add(_scale); vbox.pack_start(scale_align, true, true, 0); vbox.pack_start(_label, false, false, 0); vbox.set_size_request(100,0); // FIXME: This whole bit that makes the widget 30% of the window seems like something that // MainWindow should be doing itself // Also, it doesn't work when the window is resized small.. the slider completely disappears this.hierarchy_changed += (widget, previous_toplevel) => { if (previous_toplevel != null) { previous_toplevel.configure_event -= on_window_resize; } weak Gtk.Widget toplevel = this.get_toplevel(); if (toplevel is Gtk.Window) { toplevel.configure_event += on_window_resize; } }; Services.playback().state_changed += (playback_service) => { if (Services.playback().current_state == PlaybackState.STOPPED) { _scale.set_value(0); _scale.set_sensitive(false); _label.set_text(""); } else if (Services.playback().current_state != PlaybackState.PAUSED) { if (_timeout_stopped) { _timeout_stopped = false; GLib.Timeout.add(500, this.on_timeout); } } }; this.add(vbox); } private bool on_timeout() { // TODO deactivate the timer when the widget is not visible and reactivate // it when it is, in order to save battery life // possibly use GtkWidget visibility-notify-event? if (Services.playback().current_state == PlaybackState.STOPPED || Services.playback().current_state == PlaybackState.PAUSED) { _timeout_stopped = true; return false; } int64 pos = Services.playback().get_track_position() / 1000; int64 length = Services.playback().get_track_length() / 1000; if (length > 0) { _scale.set_range(0, length); _scale.set_value(pos); _label.set_text("%lld:%02lld of %lld:%02lld".printf( pos / 60, pos % 60, length / 60, length % 60)); } return true; } private bool on_window_resize(Gtk.Widget window, Gdk.EventConfigure event) { if (event.width/3 < 100) this.set_size_request(100, -1); else this.set_size_request(event.width/3, -1); return false; } }