~elementary-apps/audience/headerbar

« back to all changes in this revision

Viewing changes to src/Audience.vala

  • Committer: Corentin Noël
  • Date: 2014-04-28 22:27:55 UTC
  • Revision ID: tintou@mailoo.org-20140428222755-pr6ia7d6oh73krca
elementarized and first work to port it to Gtk 3.12.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        public GtkClutter.Embed           clutter;
47
47
        public Granite.Widgets.Welcome    welcome;
48
48
        public Audience.Widgets.VideoPlayer video_player;
 
49
        private Audience.Widgets.BottomBar bottom_bar;
 
50
        private Clutter.Stage stage;
 
51
        private Gtk.Overlay overlay;
49
52
 
50
53
        public bool has_dvd;
51
54
 
66
69
            settings = new Settings ();
67
70
            mainwindow = new Gtk.Window ();
68
71
            video_player = new Widgets.VideoPlayer ();
 
72
            video_player.notify["playing"].connect (() => {bottom_bar.toggle_play_pause ();});
 
73
 
69
74
            tagview = new Widgets.TagView (this);
70
 
 
71
75
            tagview.select_external_subtitle.connect (video_player.set_subtitle_uri);
72
76
 
73
 
            var mainbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
74
77
            clutter = new GtkClutter.Embed ();
75
78
 
76
79
            //prepare last played videos
99
102
            welcome.append ("media-cdrom", _("Play from Disc"), _("Watch a DVD or open a file from disc"));
100
103
            welcome.set_item_visible (2, has_dvd);
101
104
 
102
 
            var stage = clutter.get_stage ();
 
105
            stage = (Clutter.Stage)clutter.get_stage ();
103
106
 
104
107
            video_player.add_constraint (new Clutter.BindConstraint (stage, Clutter.BindCoordinate.WIDTH, 0));
105
108
            video_player.add_constraint (new Clutter.BindConstraint (stage, Clutter.BindCoordinate.HEIGHT, 0));
106
109
 
107
110
            stage.add_child (video_player);
108
111
            stage.add_child (tagview);
109
 
            stage.background_color = {0, 0, 0};
 
112
            stage.background_color = {0, 0, 0, 0};
 
113
            stage.use_alpha = true;
110
114
 
111
115
            this.tagview.y      = -10;
112
116
            this.tagview.x      = stage.width;
113
117
            this.tagview.add_constraint (new Clutter.BindConstraint (stage, Clutter.BindCoordinate.HEIGHT, -20));
114
118
 
 
119
            bottom_bar = new Widgets.BottomBar ();
 
120
            bottom_bar.set_valign (Gtk.Align.END);
 
121
            bottom_bar.run_open.connect ((type) => {run_open (type);});
 
122
            bottom_bar.play_toggled.connect (() => {video_player.playing = !video_player.playing;});
 
123
 
 
124
            overlay = new Gtk.Overlay ();
 
125
            overlay.add (clutter);
 
126
            overlay.add_overlay (bottom_bar);
 
127
 
 
128
            var mainbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
129
            mainbox.pack_start (overlay);
115
130
            mainbox.pack_start (welcome);
116
 
            mainbox.pack_start (clutter);
117
131
 
118
132
            this.mainwindow.title = program_name;
119
133
            this.mainwindow.window_position = Gtk.WindowPosition.CENTER;
124
138
            if (!settings.show_window_decoration)
125
139
                this.mainwindow.decorated = false;
126
140
 
127
 
            clutter.hide ();
 
141
            overlay.hide ();
128
142
 
129
143
            /*events*/
130
144
            video_player.text_tags_changed.connect (tagview.setup_text_setup);
131
145
            video_player.audio_tags_changed.connect (tagview.setup_audio_setup);
 
146
            video_player.progression_changed.connect ((current_time, total_time) => {
 
147
                bottom_bar.set_progression_time (current_time, total_time);
 
148
            });
132
149
 
133
150
            //look for dvd
134
151
            this.monitor = GLib.VolumeMonitor.get ();
155
172
                    break;
156
173
                case 1:
157
174
                    welcome.hide ();
158
 
                    clutter.show_all ();
 
175
                    overlay.show_all ();
159
176
 
160
177
                    open_file (filename);
161
178
 
187
204
                        open_file (entry.text, true);
188
205
                        video_player.playing = true;
189
206
                        welcome.hide ();
190
 
                        clutter.show_all ();
 
207
                        overlay.show_all ();
191
208
                    }
192
209
                    d.destroy ();
193
210
                    break;
351
368
 
352
369
            video_player.error.connect (() => {
353
370
                welcome.show_all ();
354
 
                clutter.hide ();
 
371
                overlay.hide ();
355
372
            });
356
373
 
357
374
            video_player.plugin_install_done.connect (() => {
358
 
                clutter.show ();
 
375
                overlay.show ();
359
376
                welcome.hide ();
360
377
            });
361
378
 
373
390
                }
374
391
            });
375
392
 
376
 
            video_player.configure_window.connect ((video_w, video_h) => {
377
 
 
378
 
                Gdk.Rectangle monitor;
379
 
                var screen = Gdk.Screen.get_default ();
380
 
                screen.get_monitor_geometry (
381
 
                    screen.get_monitor_at_window (mainwindow.get_window ()),
382
 
                    out monitor);
383
 
 
384
 
                int width = 0, height = 0;
385
 
                if (monitor.width > video_w && monitor.height > video_h) {
386
 
                    width = (int)video_w;
387
 
                    height = (int)video_h;
388
 
                } else {
389
 
                    width = (int)(monitor.width * 0.9);
390
 
                    height = (int)((double)video_h / video_w * width);
391
 
                }
392
 
 
393
 
                var geom = Gdk.Geometry ();
394
 
                if (settings.keep_aspect) {
395
 
                    geom.min_aspect = geom.max_aspect = video_w / (double)video_h;
396
 
                } else {
397
 
                    geom.min_aspect = 0.0;
398
 
                    geom.max_aspect = 99999999.0;
399
 
                }
400
 
 
401
 
                mainwindow.get_window ().move_resize (monitor.width / 2 - width / 2 + monitor.x,
402
 
                    monitor.height / 2 - height / 2 + monitor.y,
403
 
                    width, height);
404
 
 
405
 
                if (settings.keep_aspect) {
406
 
                    mainwindow.get_window ().set_geometry_hints (geom, Gdk.WindowHints.ASPECT);
407
 
                }
408
 
 
409
 
            });
 
393
            setup_drag_n_drop ();
 
394
            video_player.configure_window.connect ((video_w, video_h) => {on_configure_window (video_w, video_h);});
410
395
 
411
396
            //fullscreen on maximize
412
 
            mainwindow.window_state_event.connect ( (e) => {
413
 
                if (!((e.window.get_state () & Gdk.WindowState.MAXIMIZED) == 0) && !video_player.fullscreened){
414
 
                    mainwindow.fullscreen ();
415
 
                    video_player.fullscreened = true;
416
 
 
417
 
                    return true;
418
 
                }
419
 
                return false;
420
 
            });
421
 
 
422
 
            //positioning
423
 
            int old_h = - 1;
424
 
            int old_w = - 1;
425
 
            mainwindow.size_allocate.connect ( (alloc) => {
426
 
                if (alloc.width != old_w ||
427
 
                    alloc.height != old_h) {
428
 
                    if (video_player.relayout ()) {
429
 
                        old_w = alloc.width;
430
 
                        old_h = alloc.height;
431
 
                    }
432
 
                }
433
 
 
434
 
                tagview.x = tagview.expanded ? stage.width - tagview.width + 10 : stage.width;
435
 
            });
 
397
            mainwindow.window_state_event.connect ((e) => {on_window_state_changed (e.window.get_state ()); return false;});
436
398
 
437
399
            /*moving the window by drag, fullscreen for dbl-click*/
438
400
            bool down = false;
472
434
                return false;
473
435
            });
474
436
 
 
437
            mainwindow.events |= Gdk.EventMask.POINTER_MOTION_MASK;
 
438
            mainwindow.events |= Gdk.EventMask.LEAVE_NOTIFY_MASK;
 
439
            mainwindow.size_allocate.connect ((alloc) => {on_size_allocate (alloc);});
 
440
            mainwindow.motion_notify_event.connect ((event) => {return update_pointer_position (event.y, event.window.get_height ());});
 
441
            mainwindow.leave_notify_event.connect ((event) => {
 
442
                if (event.x == event.window.get_width ())
 
443
                    return update_pointer_position (event.window.get_height (), event.window.get_height ());
 
444
                if (event.x == 0)
 
445
                    return update_pointer_position (event.window.get_height (), event.window.get_height ());
 
446
                return update_pointer_position (event.y, event.window.get_height ());
 
447
            });
 
448
            //save position in video when not finished playing
 
449
            mainwindow.destroy.connect (() => {on_destroy ();});
 
450
        }
 
451
 
 
452
        private void on_configure_window (uint video_w, uint video_h) {
 
453
            Gdk.Rectangle monitor;
 
454
            var screen = Gdk.Screen.get_default ();
 
455
            screen.get_monitor_geometry (
 
456
                screen.get_monitor_at_window (mainwindow.get_window ()),
 
457
                out monitor);
 
458
 
 
459
            int width = 0, height = 0;
 
460
            if (monitor.width > video_w && monitor.height > video_h) {
 
461
                width = (int)video_w;
 
462
                height = (int)video_h;
 
463
            } else {
 
464
                width = (int)(monitor.width * 0.9);
 
465
                height = (int)((double)video_h / video_w * width);
 
466
            }
 
467
 
 
468
            var geom = Gdk.Geometry ();
 
469
            if (settings.keep_aspect) {
 
470
                geom.min_aspect = geom.max_aspect = video_w / (double)video_h;
 
471
            } else {
 
472
                geom.min_aspect = 0.0;
 
473
                geom.max_aspect = 99999999.0;
 
474
            }
 
475
 
 
476
            mainwindow.get_window ().move_resize (monitor.width / 2 - width / 2 + monitor.x,
 
477
                monitor.height / 2 - height / 2 + monitor.y,
 
478
                width, height);
 
479
 
 
480
            if (settings.keep_aspect) {
 
481
                mainwindow.get_window ().set_geometry_hints (geom, Gdk.WindowHints.ASPECT);
 
482
            }
 
483
        }
 
484
 
 
485
        private void on_window_state_changed (Gdk.WindowState window_state) {
 
486
            if (!((window_state & Gdk.WindowState.MAXIMIZED) == 0) && !video_player.fullscreened){
 
487
                mainwindow.fullscreen ();
 
488
                video_player.fullscreened = true;
 
489
            }
 
490
        }
 
491
 
 
492
        private void setup_drag_n_drop () {
475
493
            /*DnD*/
476
494
            Gtk.TargetEntry uris = {"text/uri-list", 0, 0};
477
495
            Gtk.drag_dest_set (mainwindow, Gtk.DestDefaults.ALL, {uris}, Gdk.DragAction.MOVE);
481
499
                open_file (sel.get_uris ()[0]);
482
500
 
483
501
                welcome.hide ();
484
 
                clutter.show_all ();
485
 
            });
486
 
 
487
 
            //save position in video when not finished playing
488
 
            mainwindow.destroy.connect ( () => {
489
 
                if (video_player.uri == null || video_player.uri == "" || video_player.uri.has_prefix ("dvd://"))
490
 
                    return;
491
 
                if (!video_player.at_end) {
492
 
                    for (var i = 0; i < last_played_videos.length (); i += 2){
493
 
                        if (video_player.uri == last_played_videos.nth_data (i)){
494
 
                            last_played_videos.nth (i+1).data = video_player.progress.to_string ();
495
 
                            save_last_played_videos ();
496
 
 
497
 
                            return;
498
 
                        }
499
 
                    }
500
 
                    //not in list yet, insert at start
501
 
                    last_played_videos.insert (video_player.uri, 0);
502
 
                    last_played_videos.insert (video_player.progress.to_string (), 1);
503
 
                    if (last_played_videos.length () > 10) {
504
 
                        last_played_videos.delete_link (last_played_videos.nth (10));
505
 
                        last_played_videos.delete_link (last_played_videos.nth (11));
506
 
                    }
507
 
                    save_last_played_videos ();
508
 
                }
509
 
            });
 
502
                overlay.show_all ();
 
503
            });
 
504
        }
 
505
 
 
506
        private void on_destroy () {
 
507
            if (video_player.uri == null || video_player.uri == "" || video_player.uri.has_prefix ("dvd://"))
 
508
                return;
 
509
            if (!video_player.at_end) {
 
510
                for (var i = 0; i < last_played_videos.length (); i += 2){
 
511
                    if (video_player.uri == last_played_videos.nth_data (i)){
 
512
                        last_played_videos.nth (i+1).data = video_player.progress.to_string ();
 
513
                        save_last_played_videos ();
 
514
 
 
515
                        return;
 
516
                    }
 
517
                }
 
518
                //not in list yet, insert at start
 
519
                last_played_videos.insert (video_player.uri, 0);
 
520
                last_played_videos.insert (video_player.progress.to_string (), 1);
 
521
                if (last_played_videos.length () > 10) {
 
522
                    last_played_videos.delete_link (last_played_videos.nth (10));
 
523
                    last_played_videos.delete_link (last_played_videos.nth (11));
 
524
                }
 
525
                save_last_played_videos ();
 
526
            }
 
527
        }
 
528
 
 
529
        private int old_h = - 1;
 
530
        private int old_w = - 1;
 
531
        private void on_size_allocate (Gtk.Allocation alloc) {
 
532
            if (alloc.width != old_w ||
 
533
                alloc.height != old_h) {
 
534
                if (video_player.relayout ()) {
 
535
                    old_w = alloc.width;
 
536
                    old_h = alloc.height;
 
537
                }
 
538
            }
 
539
 
 
540
            tagview.x = tagview.expanded ? stage.width - tagview.width + 10 : stage.width;
 
541
        }
 
542
 
 
543
        private bool update_pointer_position (double y, int window_height) {
 
544
            int bottom_bar_size = 0;
 
545
            int minimum = 0;
 
546
            bottom_bar.get_preferred_height (out minimum, out bottom_bar_size);
 
547
 
 
548
            if (bottom_bar.child_revealed == false)
 
549
                bottom_bar.set_reveal_child (true);
 
550
            if (y >= window_height - bottom_bar_size && y < window_height) {
 
551
                bottom_bar.hovered = true;
 
552
            } else {
 
553
                bottom_bar.hovered = false;
 
554
            }
 
555
            return false;
510
556
        }
511
557
 
512
558
        public void next_audio () {
568
614
                file.set_current_folder (settings.last_folder);
569
615
                if (file.run () == Gtk.ResponseType.ACCEPT) {
570
616
                    welcome.hide ();
571
 
                    clutter.show_all ();
 
617
                    overlay.show_all ();
572
618
                    for (var i=1;i<file.get_files ().length ();i++) {
573
619
                        playlist.add_item (file.get_files ().nth_data (i));
574
620
                    }
581
627
                video_player.playing = true;
582
628
 
583
629
                welcome.hide ();
584
 
                clutter.show_all ();
 
630
                overlay.show_all ();
585
631
            }
586
632
        }
587
633
 
658
704
 
659
705
            open_file (files[0].get_path ());
660
706
            welcome.hide ();
661
 
            clutter.show_all ();
 
707
            overlay.show_all ();
662
708
        }
663
709
    }
664
710
}
676
722
    var app = new Audience.App ();
677
723
 
678
724
    app.run (args);
679
 
}
680
 
 
 
725
}
 
 
b'\\ No newline at end of file'