SearchFilter.vala 48.3 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
2 3
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
4
 * See the COPYING file in this distribution.
5 6 7 8 9
 */

// Bitfield values used to specify which search bar features we want.
[Flags]
public enum SearchFilterCriteria {
10
    NONE = 0,
11 12 13 14
    RECURSIVE,
    TEXT,
    FLAG,
    MEDIA,
15
    RATING,
16
    SAVEDSEARCH,
17
    ALL = 0xFFFFFFFF
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
}

public enum RatingFilter {
    NO_FILTER = 0,
    REJECTED_OR_HIGHER = 1,
    UNRATED_OR_HIGHER = 2,
    ONE_OR_HIGHER = 3,
    TWO_OR_HIGHER = 4,
    THREE_OR_HIGHER = 5,
    FOUR_OR_HIGHER = 6,
    FIVE_OR_HIGHER = 7,
    REJECTED_ONLY = 8,
    UNRATED_ONLY = 9,
    ONE_ONLY = 10,
    TWO_ONLY = 11,
    THREE_ONLY = 12,
    FOUR_ONLY = 13,
    FIVE_ONLY = 14
}

 // Handles filtering via rating and text.
public abstract class SearchViewFilter : ViewFilter {
    // If this is true, allow the current rating or higher.
    private bool rating_allow_higher = true;
    
    // Rating to filter by.
44 45
    private Rating rating = Rating.UNRATED;
    private RatingFilter rating_filter = RatingFilter.UNRATED_OR_HIGHER;
46 47 48 49 50 51 52 53 54 55 56
    
    // Show flagged only if set to true.
    public bool flagged { get; set; default = false; }
    
    // Media types.
    public bool show_media_video { get; set; default = true; }
    public bool show_media_photos { get; set; default = true; }
    public bool show_media_raw { get; set; default = true; }
    
    // Search text filter.  Should only be set to lower-case.
    private string? search_filter = null;
57
    private string[]? search_filter_words = null;
58 59 60

    // Saved search filter
    public SavedSearch saved_search { get; set; default = null; }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    
    // Returns a bitmask of SearchFilterCriteria.
    // IMPORTANT: There is no signal on this, changing this value after the
    // view filter is installed will NOT update the GUI.
    public abstract uint get_criteria();
    
    public void set_rating_filter(RatingFilter rf) {
        rating_filter = rf;
        switch (rating_filter) {
            case RatingFilter.REJECTED_ONLY:
                rating = Rating.REJECTED;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.REJECTED_OR_HIGHER:
                rating = Rating.REJECTED;
                rating_allow_higher = true;
            break;
            
            case RatingFilter.ONE_OR_HIGHER:
                rating = Rating.ONE;
                rating_allow_higher = true;
            break;
            
            case RatingFilter.ONE_ONLY:
                rating = Rating.ONE;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.TWO_OR_HIGHER:
                rating = Rating.TWO;
                rating_allow_higher = true;
            break;
            
             case RatingFilter.TWO_ONLY:
                rating = Rating.TWO;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.THREE_OR_HIGHER:
                rating = Rating.THREE;
                rating_allow_higher = true;
            break;
            
            case RatingFilter.THREE_ONLY:
                rating = Rating.THREE;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.FOUR_OR_HIGHER:
                rating = Rating.FOUR;
                rating_allow_higher = true;
            break;
            
            case RatingFilter.FOUR_ONLY:
                rating = Rating.FOUR;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.FIVE_OR_HIGHER:
                rating = Rating.FIVE;
                rating_allow_higher = true;
            break;
            
            case RatingFilter.FIVE_ONLY:
                rating = Rating.FIVE;
                rating_allow_higher = false;
            break;
            
            case RatingFilter.UNRATED_OR_HIGHER:
            default:
                rating = Rating.UNRATED;
                rating_allow_higher = true;
            break;
        }
    }
    
138 139 140 141 142
    public bool has_search_filter() {
        return !is_string_empty(search_filter);
    }
    
    public unowned string? get_search_filter() {
143 144
        return search_filter;
    }
145
       
146 147 148
    public unowned string[]? get_search_filter_words() {
        return search_filter_words;
    }
149
    
150
    public void set_search_filter(string? text) {
151
        search_filter = !is_string_empty(text) ? String.remove_diacritics(text.down()) : null;
152
        search_filter_words = search_filter != null ? search_filter.split(" ") : null;
153 154 155 156
    }
    
    public void clear_search_filter() {
        search_filter = null;
157
        search_filter_words = null;
158 159
    }
    
160 161 162 163
    public bool has_saved_search() {
        return saved_search != null;
    }

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    public bool get_rating_allow_higher() {
        return rating_allow_higher;
    }
    
    public Rating get_rating() {
        return rating;
    }
    
    public bool filter_by_media_type() {
        return ((show_media_video || show_media_photos || show_media_raw) && 
            !(show_media_video && show_media_photos && show_media_raw));
    }
}

// This class provides a default predicate implementation used for CollectionPage
// as well as Trash and Offline.
public abstract class DefaultSearchViewFilter : SearchViewFilter {
    public override bool predicate(DataView view) {
        MediaSource source = ((Thumbnail) view).get_media_source();
183 184
        uint criteria = get_criteria();
        
185
        // Ratings filter
186
        if ((SearchFilterCriteria.RATING & criteria) != 0) {
187 188 189 190 191 192 193
            if (get_rating_allow_higher() && source.get_rating() < get_rating())
                return false;
            else if (!get_rating_allow_higher() && source.get_rating() != get_rating())
                return false;
        }
        
        // Flag state.
194
        if ((SearchFilterCriteria.FLAG & criteria) != 0) {
195 196 197 198 199
            if (flagged && source is Flaggable && !((Flaggable) source).is_flagged())
                return false;
        }
        
        // Media type.
200
        if (((SearchFilterCriteria.MEDIA & criteria) != 0) && filter_by_media_type()) {
201 202 203 204
            if (source is VideoSource) {
                if (!show_media_video)
                    return false;
            } else if (source is Photo) {
205 206
                Photo photo = source as Photo;
                if (photo.get_master_file_format() == PhotoFileFormat.RAW) {
207
                    if (!show_media_photos && !show_media_raw)
208 209 210 211 212 213
                        return false;
                } else if (!show_media_photos)
                    return false;
            }
        }
        
214
        // Text
215 216 217 218 219 220 221 222 223 224
        if (((SearchFilterCriteria.TEXT & criteria) != 0) && has_search_filter()) {
            unowned string? media_keywords = source.get_indexable_keywords();
            
            unowned string? event_keywords = null;
            Event? event = source.get_event();
            if (event != null)
                event_keywords = event.get_indexable_keywords();
            
            Gee.List<Tag>? tags = Tag.global.fetch_for_source(source);
            int tags_size = (tags != null) ? tags.size : 0;
225
 
226 227 228 229
#if ENABLE_FACES           
            Gee.List<Face>? faces = Face.global.fetch_for_source(source);
#endif
            
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            foreach (unowned string word in get_search_filter_words()) {
                if (media_keywords != null && media_keywords.contains(word))
                    continue;
                
                if (event_keywords != null && event_keywords.contains(word))
                    continue;
                
                if (tags_size > 0) {
                    bool found = false;
                    for (int ctr = 0; ctr < tags_size; ctr++) {
                        unowned string? tag_keywords = tags[ctr].get_indexable_keywords();
                        if (tag_keywords != null && tag_keywords.contains(word)) {
                            found = true;
                            
                            break;
                        }
                    }
                    
248 249 250 251
                    if (found)
                        continue;
                }
                
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
#if ENABLE_FACES
                if (faces != null) {
                    bool found = false;
                    foreach (Face f in faces) {
                        unowned string? face_keywords = f.get_indexable_keywords();
                        if (face_keywords != null && face_keywords.contains(word)) {
                            found = true;
                            
                            break;
                        }
                    }
                    
                    if (found)
                        continue;
                }
#endif                
268 269 270
                // failed all tests (this even works if none of the Indexables have strings,
                // as they fail the implicit AND test)
                return false;
271
            }
272 273
        }
        
274 275 276 277 278
        // Saved search
        if (((SearchFilterCriteria.SAVEDSEARCH & criteria) != 0) && has_saved_search()) {
            return saved_search.predicate(source);
        }

279 280
        return true;
    }
281 282
}

283 284 285 286 287 288 289 290 291 292
public class DisabledViewFilter : SearchViewFilter {
    public override bool predicate(DataView view) {
        return true;
    }
    
    public override uint get_criteria() {
        return SearchFilterCriteria.RATING;
    }
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
public class TextAction {
    public string? value {
        get {
            return text;
        }
    }
    
    private string? text = null;
    private bool sensitive = true;
    private bool visible = true;
    
    public signal void text_changed(string? text);
    
    public signal void sensitivity_changed(bool sensitive);
    
    public signal void visibility_changed(bool visible);
    
    public TextAction(string? init = null) {
        text = init;
    }
    
    public void set_text(string? text) {
        if (this.text != text) {
            this.text = text;
            text_changed(text);
        }
    }
    
    public void clear() {
        set_text(null);
    }
    
    public bool is_sensitive() {
        return sensitive;
    }
328
    
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    public void set_sensitive(bool sensitive) {
        if (this.sensitive != sensitive) {
            this.sensitive = sensitive;
            sensitivity_changed(sensitive);
        }
    }
    
    public bool is_visible() {
        return visible;
    }
    
    public void set_visible(bool visible) {
        if (this.visible != visible) {
            this.visible = visible;
            visibility_changed(visible);
        }
    }
346 347
}

348 349

public class SearchFilterActions {
350
    public unowned GLib.SimpleAction? flagged {
351
        get {
352
            return get_action ("display.flagged");
353 354 355
        }
    }
    
356
    public unowned GLib.SimpleAction? photos {
357
        get {
358
            return get_action ("display.photos");
359 360 361
        }
    }
    
362
    public unowned GLib.SimpleAction? videos {
363
        get {
364
            return get_action ("display.videos");
365 366 367
        }
    }
    
368
    public unowned GLib.SimpleAction? raw {
369
        get {
370
            return get_action ("display.raw");
371 372 373
        }
    }
    
374
    public unowned GLib.SimpleAction? rating {
375
        get {
376
            return get_action ("display.rating");
377 378 379
        }
    }
    
380 381 382 383 384 385 386 387 388 389 390 391 392
    public unowned TextAction text {
        get {
            assert(_text != null);
            return _text;
        }
    }
    
    private SearchFilterCriteria criteria = SearchFilterCriteria.ALL;
    private TextAction? _text = null;
    private bool has_flagged = true;
    private bool has_photos = true;
    private bool has_videos = true;
    private bool has_raw = true;
393
    private bool can_filter_by_stars = true;
394 395 396 397 398 399 400 401 402 403 404
    
    public signal void flagged_toggled(bool on);
    
    public signal void photos_toggled(bool on);
    
    public signal void videos_toggled(bool on);
    
    public signal void raw_toggled(bool on);
    
    public signal void rating_changed(RatingFilter filter);
    
405
    public signal void text_changed(string? text);
406

407
    
408 409 410 411 412 413 414 415
    /**
     * fired when the kinds of media present in the current view change (e.g., a video becomes
     * available in the view through a new import operation or no raw photos are available in
     * the view anymore because the last one was moved to the trash)
     */
    public signal void media_context_changed(bool has_photos, bool has_videos, bool has_raw,
        bool has_flagged);
    
416 417 418 419 420 421
    // Ticket #3290 - Hide some search bar fields when they
    // cannot be used.
    // Part 1 - we use this to announce when the criteria have changed,
    // and the toolbar can listen for it and hide or show widgets accordingly.
    public signal void criteria_changed();
    
422
    public SearchFilterActions() {
423 424 425
        // the getters defined above should not be used until register() returns
        register();
        
426
        text.text_changed.connect(on_text_changed);
427 428
    }
    
429 430 431
    public SearchFilterCriteria get_criteria() {
        return criteria;
    }
432 433 434 435

    public unowned GLib.ActionEntry[] get_actions () {
        return SearchFilterActions.entries;
    }
436
    
437 438 439 440 441 442 443 444
    public unowned GLib.SimpleAction? get_action(string name) {
        var lw = AppWindow.get_instance () as LibraryWindow;
        if (lw != null) {
            return lw.lookup_action (name) as GLib.SimpleAction;
        }

        return null;
        //    return action_group.lookup_action(name) as GLib.SimpleAction;
445 446
    }
    
447 448 449 450 451
    public void set_action_sensitive (string name, bool sensitive) {
        var action = get_action(name);
        if (action != null) {
            action.set_enabled (sensitive);
        }
452 453
    }
    
454
    public void reset() {
455 456 457 458
        flagged.change_state (false);
        photos.change_state (false);
        raw.change_state (false);
        videos.change_state (false);
459
        Variant v = "%d".printf (RatingFilter.UNRATED_OR_HIGHER);
460
        rating.change_state (v);
461

462 463 464 465 466 467
        text.set_text(null);
    }
    
    public void set_sensitive_for_search_criteria(SearchFilterCriteria criteria) {
        this.criteria = criteria;
        update_sensitivities();
468 469 470
        
        // Announce that we've gotten a new criteria...
        criteria_changed();
471 472 473 474 475
    }
    
    public void monitor_page_contents(Page? old_page, Page? new_page) {
        CheckerboardPage? old_tracked_page = old_page as CheckerboardPage;
        if (old_tracked_page != null) {
476
            Core.ViewTracker? tracker = old_tracked_page.get_view_tracker();
477 478 479 480 481 482 483 484
            if (tracker is MediaViewTracker)
                tracker.updated.disconnect(on_media_tracker_updated);
            else if (tracker is CameraViewTracker)
                tracker.updated.disconnect(on_camera_tracker_updated);
        }
        
        CheckerboardPage? new_tracked_page = new_page as CheckerboardPage;
        if (new_tracked_page != null) {
485 486
            can_filter_by_stars = true;
            
487
            Core.ViewTracker? tracker = new_tracked_page.get_view_tracker();
488 489 490 491 492 493 494 495 496 497 498 499 500
            if (tracker is MediaViewTracker) {
                tracker.updated.connect(on_media_tracker_updated);
                on_media_tracker_updated(tracker);
                
                return;
            } else if (tracker is CameraViewTracker) {
                tracker.updated.connect(on_camera_tracker_updated);
                on_camera_tracker_updated(tracker);
                
                return;
            }
        }
        
501
        // go with default behavior of making none of the filters available.
502 503 504 505
        has_flagged = false;
        has_photos = false;
        has_videos = false;
        has_raw = false;
506
        can_filter_by_stars = false;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        
        update_sensitivities();
    }
    
    private void on_media_tracker_updated(Core.Tracker t) {
        MediaViewTracker tracker = (MediaViewTracker) t;
        
        has_flagged = tracker.all.flagged > 0;
        has_photos = tracker.all.photos > 0;
        has_videos = tracker.all.videos > 0;
        has_raw = tracker.all.raw > 0;
        
        update_sensitivities();
    }
    
    private void on_camera_tracker_updated(Core.Tracker t) {
        CameraViewTracker tracker = (CameraViewTracker) t;
        
        has_flagged = false;
        has_photos = tracker.all.photos > 0;
        has_videos = tracker.all.videos > 0;
        has_raw = tracker.all.raw > 0;
529

530 531 532 533
        update_sensitivities();
    }
    
    private void update_sensitivities() {
534
        bool allow_ratings = (SearchFilterCriteria.RATING & criteria) != 0;
535 536
        set_action_sensitive("display.rating", allow_ratings & can_filter_by_stars);

537 538 539
        // Ticket #3343 - Don't disable the text field, even
        // when no searchable items are available.
        text.set_sensitive(true);
540 541
        
        media_context_changed(has_photos, has_videos, has_raw, has_flagged);
542 543
    }
    
544 545 546
    private void on_text_changed(TextAction action, string? text) {
        text_changed(text);
    }
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

    private const GLib.ActionEntry[] entries = {
        { "display.rating", on_action_radio, "s", "'2'", on_rating_changed },
        { "display.flagged", on_action_toggle, null, "false", on_flagged_toggled },
        { "display.photos", on_action_toggle, null, "false", on_photos_toggled },
        { "display.videos", on_action_toggle, null, "false", on_videos_toggled },
        { "display.raw", on_action_toggle, null, "false", on_raw_toggled }
    };

    private void on_action_radio (GLib.SimpleAction action,
                                  GLib.Variant?     parameter) {
        action.change_state (parameter);
    }

    private void on_action_toggle (GLib.SimpleAction action,
                                   GLib.Variant?     parameter) {
        var state = (bool) action.get_state ();
        action.change_state (!state);
    }
566
    
567
    private void register() {
568
        _text = new TextAction();
569
    }
570 571 572

    private void on_rating_changed (GLib.SimpleAction action,
                                    GLib.Variant      value) {
Jens Georg's avatar
Jens Georg committed
573 574 575
        if (value.get_string () == action.get_state().get_string ())
            return;

576
        var filter = (RatingFilter) int.parse (value.get_string ());
Jens Georg's avatar
Jens Georg committed
577
        action.set_state (value);
578
        rating_changed(filter);
579 580
    }
    
581 582 583 584
    private void on_flagged_toggled (GLib.SimpleAction action,
                                     GLib.Variant      value) {
        action.set_state (value);
        flagged_toggled (value.get_boolean ());
585 586
    }
    
587 588 589 590
    private void on_photos_toggled (GLib.SimpleAction action,
                                    GLib.Variant      value) {
        action.set_state (value);
        photos_toggled (value.get_boolean ());
591 592
    }
    
593 594 595 596
    private void on_videos_toggled (GLib.SimpleAction action,
                                    GLib.Variant      value) {
        action.set_state (value);
        videos_toggled (value.get_boolean ());
597 598
    }
    
599 600 601 602
    private void on_raw_toggled (GLib.SimpleAction action,
                                 GLib.Variant      value) {
        action.set_state (value);
        raw_toggled (value.get_boolean ());
603
    }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    
    public bool get_has_photos() {
        return has_photos;
    }
    
    public bool get_has_videos() {
        return has_videos;
    }
    
    public bool get_has_raw() {
        return has_raw;
    }
    
    public bool get_has_flagged() {
        return has_flagged;
    }
620
}
621

622 623
public class SearchFilterToolbar : Gtk.Revealer {
    private Gtk.Toolbar toolbar;
624 625 626 627 628 629 630 631
    private const int FILTER_BUTTON_MARGIN = 12; // the distance between icon and edge of button
    private const float FILTER_ICON_STAR_SCALE = 0.65f; // changes the size of the filter icon
    private const float FILTER_ICON_SCALE = 0.75f; // changes the size of the all photos icon
    
    // filter_icon_base_width is the width (in px) of a single filter icon such as one star or an "X"
    private const int FILTER_ICON_BASE_WIDTH = 30;
    // filter_icon_plus_width is the width (in px) of the plus icon
    private const int FILTER_ICON_PLUS_WIDTH = 20;
632 633
    
    private class LabelToolItem : Gtk.ToolItem {
634 635
        private Gtk.Label label;
        
636
        public LabelToolItem(string s, int left_padding = 0, int right_padding = 0) {
637
            label = new Gtk.Label(s);
638
            if (left_padding != 0 || right_padding != 0) {
639 640 641 642
                label.halign = Gtk.Align.START;
                label.valign = Gtk.Align.CENTER;
                label.margin_start = left_padding;
                label.margin_end = right_padding;
643
            }
644
            add (label);
645 646 647
        }
    }
    
648 649 650
    private class ToggleActionToolButton : Gtk.ToolItem {
        private Gtk.ToggleButton button;

651
        public ToggleActionToolButton(string action) {
652 653
            button = new Gtk.ToggleButton();
            button.set_can_focus(false);
654
            button.set_action_name (action);
655
            button.set_has_tooltip(true);
656 657
            button.set_relief(Gtk.ReliefStyle.NONE);
            button.set_margin_start(2);
658 659 660 661 662 663
            
            this.add(button);
        }
        
        public void set_icon_name(string icon_name) {
            Gtk.Image? image = null;
664
            button.set_always_show_image(true);
Jens Georg's avatar
Jens Georg committed
665
            image = new Gtk.Image.from_icon_name(icon_name, Gtk.IconSize.SMALL_TOOLBAR);
666
            button.set_image(image);
667 668 669
            if (button.get_label() != "" && button.get_label() != null) {
                image.margin_end = 6;
            }
670
        }
671 672 673 674 675

        public void set_label(string label) {
            button.set_label(label);
        }

676 677
    }
    
678 679 680 681
    // Ticket #3260 - Add a 'close' context menu to
    // the searchbar.
    // The close menu. Populated below in the constructor.
    private Gtk.Menu close_menu = new Gtk.Menu();
682
    private Gtk.MenuItem close_item = new Gtk.MenuItem();
683

684 685
    // Text search box.
    protected class SearchBox : Gtk.ToolItem {
686 687
        private Gtk.SearchEntry search_entry;
        private TextAction action;
688 689
        
        public SearchBox(TextAction action) {
690 691 692 693 694 695 696 697
            this.action = action;
            search_entry = new Gtk.SearchEntry();
            
            search_entry.width_chars = 23;
            search_entry.key_press_event.connect(on_escape_key); 
            add(search_entry);
            
            set_nullable_text(action.value);
698
            
699 700 701 702 703 704
            action.text_changed.connect(on_action_text_changed);
            action.sensitivity_changed.connect(on_sensitivity_changed);
            action.visibility_changed.connect(on_visibility_changed);
            
            search_entry.buffer.deleted_text.connect(on_entry_changed);
            search_entry.buffer.inserted_text.connect(on_entry_changed);
705 706 707
        }
        
        ~SearchBox() {
708 709 710 711 712 713
            action.text_changed.disconnect(on_action_text_changed);
            action.sensitivity_changed.disconnect(on_sensitivity_changed);
            action.visibility_changed.disconnect(on_visibility_changed);
            
            search_entry.buffer.deleted_text.disconnect(on_entry_changed);
            search_entry.buffer.inserted_text.disconnect(on_entry_changed);
714 715 716
        }
        
        public void get_focus() {
717
            search_entry.has_focus = true;
718 719
        }
        
720 721 722
        // Ticket #3124 - user should be able to clear 
        // the search textbox by typing 'Esc'. 
        private bool on_escape_key(Gdk.EventKey e) { 
723
            if(Gdk.keyval_name(e.keyval) == "Escape")
724
                action.clear();
725
            
726 727
           // Continue processing this event, since the 
           // text entry functionality needs to see it too. 
728 729
            return false; 
        }
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        
        private void on_action_text_changed(string? text) {
            search_entry.buffer.deleted_text.disconnect(on_entry_changed);
            search_entry.buffer.inserted_text.disconnect(on_entry_changed);
            set_nullable_text(text);
            search_entry.buffer.deleted_text.connect(on_entry_changed);
            search_entry.buffer.inserted_text.connect(on_entry_changed);
        }
        
        private void on_entry_changed() {
            action.text_changed.disconnect(on_action_text_changed);
            action.set_text(search_entry.get_text());
            action.text_changed.connect(on_action_text_changed);
        }
        
        private void on_sensitivity_changed(bool sensitive) {
            this.sensitive = sensitive;
        }
        
        private void on_visibility_changed(bool visible) {
            ((Gtk.Widget) this).visible = visible;
        }
        
        private void set_nullable_text(string? text) {
            search_entry.set_text(text != null ? text : "");
        }
756 757 758
    }
    
    // Handles ratings filters.
759
    protected class RatingFilterButton : Gtk.ToolItem {
760 761 762 763 764 765
        public Gtk.MenuButton button;

        public RatingFilterButton(GLib.MenuModel model) {
            button = new Gtk.MenuButton();

            button.set_image (get_filter_icon(RatingFilter.UNRATED_OR_HIGHER));
766
            button.set_can_focus(false);
767 768
            button.set_relief(Gtk.ReliefStyle.NONE);
            button.set_margin_start(2);
769
            button.set_menu_model (model);
770

771
            set_homogeneous(false);
772

773 774
            this.add(button);
        }
775

776
        private Gtk.Widget get_filter_icon(RatingFilter filter) {
Jens Georg's avatar
Jens Georg committed
777
            Gtk.Widget? icon = null;
778 779 780

            switch (filter) {
                case RatingFilter.REJECTED_OR_HIGHER:
Jens Georg's avatar
Jens Georg committed
781 782 783 784 785 786 787
                    icon = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
                    var image = new Gtk.Image.from_icon_name ("emblem-photos-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
                    image.margin_end = 2;
                    (icon as Gtk.Box).pack_start(image);
                    image = new Gtk.Image.from_icon_name ("window-close-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
                    (icon as Gtk.Box).pack_start(image);
                    icon.show_all();
788 789 790
                break;
                
                case RatingFilter.REJECTED_ONLY:
Jens Georg's avatar
Jens Georg committed
791
                    icon = new Gtk.Image.from_icon_name ("window-close-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
792 793 794 795
                break;
                
                case RatingFilter.UNRATED_OR_HIGHER:
                default:
Jens Georg's avatar
Jens Georg committed
796
                    icon = new Gtk.Image.from_icon_name ("emblem-photos-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
797 798
                break;
            }
799

Jens Georg's avatar
Jens Georg committed
800 801 802
            icon.margin_end = 6;

            return icon;
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
        }

        private int get_filter_icon_size(RatingFilter filter) {
            int icon_base = (int) (FILTER_ICON_BASE_WIDTH * FILTER_ICON_SCALE);
            int icon_star_base = (int) (FILTER_ICON_BASE_WIDTH * FILTER_ICON_STAR_SCALE);
            int icon_plus = (int) (FILTER_ICON_PLUS_WIDTH * FILTER_ICON_STAR_SCALE);
            
            switch (filter) {
                case RatingFilter.ONE_OR_HIGHER:
                    return icon_star_base + icon_plus;
                case RatingFilter.TWO_OR_HIGHER:
                    return icon_star_base * 2 + icon_plus;
                case RatingFilter.THREE_OR_HIGHER:
                    return icon_star_base * 3 + icon_plus;
                case RatingFilter.FOUR_OR_HIGHER:
                    return icon_star_base * 4 + icon_plus;
                case RatingFilter.FIVE_OR_HIGHER:
                case RatingFilter.FIVE_ONLY:
                    return icon_star_base * 5;
                case RatingFilter.REJECTED_OR_HIGHER:
                    return Resources.ICON_FILTER_REJECTED_OR_BETTER_FIXED_SIZE;
                case RatingFilter.UNRATED_OR_HIGHER:
                    return Resources.ICON_FILTER_UNRATED_OR_BETTER_FIXED_SIZE;
                case RatingFilter.REJECTED_ONLY:
                    return icon_plus;
                default:
                    return icon_base;
            }
        }

        public void set_filter_icon(RatingFilter filter) {
834
            button.set_always_show_image(true);
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
            switch (filter) {
            case RatingFilter.ONE_OR_HIGHER:
                button.set_label (_("★+ Rating"));
                break;
            case RatingFilter.TWO_OR_HIGHER:
                button.set_label (_("★★+ Rating"));
                break;
            case RatingFilter.THREE_OR_HIGHER:
                button.set_label (_("★★★+ Rating"));
                break;
            case RatingFilter.FOUR_OR_HIGHER:
                button.set_label (_("★★★★+ Rating"));
                break;
            case RatingFilter.FIVE_ONLY:
            case RatingFilter.FIVE_OR_HIGHER:
                button.set_label (_("★★★★★+ Rating"));
                break;
            default:
                button.set_label (_("Rating"));
                button.set_image(get_filter_icon(filter));
                break;
            }

858 859
            set_size_request(get_filter_button_size(filter), -1);
            set_tooltip_text(Resources.get_rating_filter_tooltip(filter));
860
            set_has_tooltip(true);
861 862 863 864 865 866
            show_all();
        }

        private int get_filter_button_size(RatingFilter filter) {
            return get_filter_icon_size(filter) + 2 * FILTER_BUTTON_MARGIN;
        }
867 868 869 870 871

        public void set_label(string label) {
            button.set_label(label);
        }

872
    }
873 874 875 876 877 878 879 880 881 882 883

    protected class SavedSearchFilterButton : Gtk.ToolItem {
        public SavedSearchPopover filter_popup = null;
        public Gtk.ToggleButton button;

        public signal void clicked();

        public SavedSearchFilterButton() {
            button = new Gtk.ToggleButton();
            button.set_always_show_image(true);

884
            Gtk.Image? image = new Gtk.Image.from_icon_name("edit-find-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
            image.set_margin_end(6);
            button.set_image(image);
            button.set_can_focus(false);

            button.clicked.connect(on_clicked);

            restyle();

            set_homogeneous(false);

            this.add(button);
        }

        ~SavedSearchFilterButton() {
            button.clicked.disconnect(on_clicked);
        }

        private void on_clicked() {
            clicked();
        }

        public void set_active(bool active) {
            button.set_active(active);
        }

        public void set_label(string label) {
            button.set_label(label);
        }

        public void restyle() {
			button.set_size_request(24, 24);
			button.relief = Gtk.ReliefStyle.NONE;
        }
    }

    protected class SavedSearchPopover {
        private Gtk.Popover popover = null;
        private Gtk.ListBox list_box = null;
        private DataButton[] edit_buttons = null;
        private DataButton[] delete_buttons = null;
        Gtk.Button add = null;

        public signal void search_activated(SavedSearch search);
        public signal void edit_clicked(SavedSearch search);
        public signal void delete_clicked(SavedSearch search);
        public signal void add_clicked();

        public signal void closed();

        private class DataButton : Gtk.Bin {
            private Gtk.Button button = null;
            public SavedSearch search { get; private set; }

            public signal void clicked(SavedSearch search);

            public DataButton(SavedSearch search, string name) {
                button = new Gtk.Button.from_icon_name(name, Gtk.IconSize.SMALL_TOOLBAR);
                this.search = search;
                this.add(button);

                restyle();

                button.clicked.connect(on_click);
            }

            ~DataButton() {
                button.clicked.disconnect(on_click);
            }

            public void restyle() {
                button.set_size_request(24, 24);
                button.relief = Gtk.ReliefStyle.NONE;
            }

            private void on_click() {
                clicked(this.search);
            }
        }

        public SavedSearchPopover(Gtk.Widget relative_to) {
            popover = new Gtk.Popover(relative_to);
            popover.closed.connect(on_popover_closed);
            list_box = new Gtk.ListBox();
            edit_buttons = new DataButton[0];
            delete_buttons = new DataButton[0];

            foreach (SavedSearch search in SavedSearchTable.get_instance().get_all()) {
                Gtk.Box row = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 1);
                row.set_homogeneous(false);
                Gtk.Label label = new Gtk.Label(search.get_name());
                label.halign = Gtk.Align.START;
                row.pack_start(label, true, true, 3);

                DataButton delete_button = new DataButton(search, "edit-delete-symbolic");
                row.pack_end(delete_button, false, false);
                delete_button.clicked.connect(on_delete_click);
                delete_buttons += delete_button;

                DataButton edit_button = new DataButton(search, "text-editor-symbolic");
                row.pack_end(edit_button, false, false);
                edit_button.clicked.connect(on_edit_click);
                edit_buttons += edit_button;

                list_box.insert(row, -1);
            }
            add = new Gtk.Button.from_icon_name("list-add-symbolic", Gtk.IconSize.BUTTON);
            add.clicked.connect(on_add_click);
            list_box.insert(add, -1);
            list_box.row_activated.connect(on_activate_row);
            list_box.selection_mode = Gtk.SelectionMode.NONE;
            popover.add(list_box);

            restyle();
        }

        ~SavedSearchPopover() {
            foreach (DataButton button in edit_buttons) button.clicked.disconnect(on_edit_click);
            foreach (DataButton button in delete_buttons) button.clicked.disconnect(on_delete_click);
            add.clicked.disconnect(on_add_click);
            list_box.row_activated.disconnect(on_activate_row);
            popover.closed.disconnect(on_popover_closed);
        }

        public void restyle() {
            add.relief = Gtk.ReliefStyle.NONE;
            foreach (DataButton button in edit_buttons) button.restyle();
            foreach (DataButton button in delete_buttons) button.restyle();
        }

        private bool is_search_row(Gtk.ListBoxRow? row) {
            if (row == null) return false;
            if (row.get_children().last().data is Gtk.Button) return false;
            return true;
        }

        private SavedSearch get_search(Gtk.ListBoxRow row) {
            DataButton button = (row.get_children().first().data as Gtk.Box).get_children().last().data as DataButton;
            return button.search;
        }

        private void on_activate_row(Gtk.ListBoxRow? row) {
            if (is_search_row(row))
                search_activated(get_search(row));
            popover.hide();
        }

        private void on_edit_click(SavedSearch search) {
            edit_clicked(search);
        }

        private void on_delete_click(SavedSearch search) {
            delete_clicked(search);
        }

        private void on_add_click() {
            add_clicked();
        }

        private void on_popover_closed() {
            closed();
        }

        public void show_all() {
            popover.show_all();
        }

        public void hide() {
            popover.hide();
        }
    }
1055
    
1056
    public Gtk.Builder builder = new Gtk.Builder ();
1057
    
1058
    private SearchFilterActions actions;
1059
    private SavedSearch saved_search = null;
1060
    private SearchBox search_box;
1061
    private RatingFilterButton rating_button;
1062 1063
    private SavedSearchFilterButton saved_search_button = new SavedSearchFilterButton();
    private bool elide_showing_again = false;
1064
    private SearchViewFilter? search_filter = null;
1065
    private LabelToolItem label_type;
1066 1067 1068 1069 1070 1071
    private ToggleActionToolButton toolbtn_photos;
    private ToggleActionToolButton toolbtn_videos;
    private ToggleActionToolButton toolbtn_raw;
    private ToggleActionToolButton toolbtn_flag;
    private Gtk.SeparatorToolItem sepr_mediatype_flagged;
    private Gtk.SeparatorToolItem sepr_flagged_rating;
1072
    private Gtk.SeparatorToolItem sepr_rating_saved;
1073
    
1074 1075
    public SearchFilterToolbar(SearchFilterActions actions) {
        this.actions = actions;
1076
        toolbar = new Gtk.Toolbar();
1077
        actions.media_context_changed.connect(on_media_context_changed);
1078 1079
        search_box = new SearchBox(actions.text);
        
1080 1081
        toolbar.set_name("search-filter-toolbar");
        toolbar.set_icon_size(Gtk.IconSize.SMALL_TOOLBAR);
1082 1083
        
        try {
1084
            this.builder.add_from_resource (Resources.get_ui("search_bar.ui"));
1085
        } catch (Error err) {
1086 1087
            AppWindow.panic(_("Error loading search bar UI: %s").printf(
                err.message));
1088 1089
        }
        
1090 1091 1092 1093 1094
        // Ticket #3260 - Add a 'close' context menu to
        // the searchbar.
        // Prepare the close menu for use, but don't
        // display it yet; we'll connect it to secondary
        // click later on.
1095 1096
        close_item.set_label(_("Close"));
        close_item.show();
1097 1098 1099
        close_item.activate.connect(on_context_menu_close_chosen);
        close_menu.append(close_item);
       
1100
        // Type label and toggles
1101
        label_type = new LabelToolItem(_("Type"), 10, 5);
1102
        toolbar.insert(label_type, -1);
1103
        
1104 1105
        toolbtn_photos = new ToggleActionToolButton("win.display.photos");
        toolbtn_photos.set_tooltip_text (_("Photos"));
1106
        
1107 1108
        toolbtn_videos = new ToggleActionToolButton("win.display.videos");
        toolbtn_videos.set_tooltip_text(_("Videos"));
1109
        
1110 1111
        toolbtn_raw = new ToggleActionToolButton("win.display.raw");
        toolbtn_raw.set_tooltip_text(_("RAW Photos"));
1112
        
1113 1114 1115
        toolbar.insert(toolbtn_photos, -1);
        toolbar.insert(toolbtn_videos, -1);
        toolbar.insert(toolbtn_raw, -1);
1116 1117
        
        // separator
1118
        sepr_mediatype_flagged = new Gtk.SeparatorToolItem();
1119
        toolbar.insert(sepr_mediatype_flagged, -1);
1120
        
1121
        // Flagged button
1122
        
1123
        toolbtn_flag = new ToggleActionToolButton("win.display.flagged");
1124
        toolbtn_flag.set_label(_("Flagged"));
1125
        toolbtn_flag.set_tooltip_text(_("Flagged"));
1126
        
1127
        toolbar.insert(toolbtn_flag, -1);
1128 1129
        
        // separator
1130
        sepr_flagged_rating = new Gtk.SeparatorToolItem();
1131
        toolbar.insert(sepr_flagged_rating, -1);
1132
        
1133
        // Rating button
1134 1135
        var model = this.builder.get_object ("popup-menu") as GLib.MenuModel;
        rating_button = new RatingFilterButton (model);
1136
        rating_button.set_label(_("Rating"));
1137
        rating_button.set_expand(false);
1138
        toolbar.insert(rating_button, -1);
1139
        
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
        // separator
        sepr_rating_saved = new Gtk.SeparatorToolItem();
        toolbar.insert(sepr_rating_saved, -1);

        // Saved search button
        saved_search_button.set_expand(false);
		saved_search_button.set_label(_("Saved Search"));
        saved_search_button.set_tooltip_text(_("Use a saved search to filter items in the current view"));
        saved_search_button.clicked.connect(on_saved_search_button_clicked);
        toolbar.insert(saved_search_button, -1);

1151 1152 1153 1154
        // Separator to right-align the text box
        Gtk.SeparatorToolItem separator_align = new Gtk.SeparatorToolItem();
        separator_align.set_expand(true);
        separator_align.set_draw(false);
1155
        toolbar.insert(separator_align, -1);
1156 1157
        
        // Search box.
1158 1159 1160
        toolbar.insert(search_box, -1);

        add(toolbar);
1161
        
1162 1163 1164 1165 1166 1167
        // hook up signals to actions to be notified when they change
        actions.flagged_toggled.connect(on_flagged_toggled);
        actions.photos_toggled.connect(on_photos_toggled);
        actions.videos_toggled.connect(on_videos_toggled);
        actions.raw_toggled.connect(on_raw_toggled);
        actions.rating_changed.connect(on_rating_changed);
1168
        actions.text_changed.connect(on_search_text_changed);
1169
        actions.criteria_changed.connect(on_criteria_changed);
1170 1171
        
        // #3260 part II Hook up close menu.
1172
        toolbar.popup_context_menu.connect(on_context_menu_requested);
1173 1174 1175
        
        on_media_context_changed(actions.get_has_photos(), actions.get_has_videos(),
            actions.get_has_raw(), actions.get_has_flagged());
1176 1177
    }
    
1178
    ~SearchFilterToolbar() {
1179 1180
        
        actions.media_context_changed.disconnect(on_media_context_changed);
1181

1182 1183 1184 1185 1186
        actions.flagged_toggled.disconnect(on_flagged_toggled);
        actions.photos_toggled.disconnect(on_photos_toggled);
        actions.videos_toggled.disconnect(on_videos_toggled);
        actions.raw_toggled.disconnect(on_raw_toggled);
        actions.rating_changed.disconnect(on_rating_changed);
1187
        actions.text_changed.disconnect(on_search_text_changed);
1188
        actions.criteria_changed.disconnect(on_criteria_changed);
1189
        
1190
        toolbar.popup_context_menu.disconnect(on_context_menu_requested); 
1191 1192
    }
    
1193 1194
    private void on_media_context_changed(bool has_photos, bool has_videos, bool has_raw,
        bool has_flagged) {
1195 1196
        if (has_photos || has_raw)
            // As a user, I would expect, that a raw photo is still a photo.
1197
            // Let's enable the photo button even if there are only raw photos.
Jens Georg's avatar
Jens Georg committed
1198
            toolbtn_photos.set_icon_name("filter-photos-symbolic");
1199
        else
Jens Georg's avatar
Jens Georg committed
1200
            toolbtn_photos.set_icon_name("filter-photos-disabled-symbolic");
1201 1202

        if (has_videos)
Jens Georg's avatar
Jens Georg committed
1203
            toolbtn_videos.set_icon_name("filter-videos-symbolic");
1204
        else
Jens Georg's avatar
Jens Georg committed
1205
            toolbtn_videos.set_icon_name("filter-videos-disabled-symbolic");
1206 1207

        if (has_raw)
Jens Georg's avatar
Jens Georg committed
1208
            toolbtn_raw.set_icon_name("filter-raw-symbolic");
1209
        else
Jens Georg's avatar
Jens Georg committed
1210
            toolbtn_raw.set_icon_name("filter-raw-disabled-symbolic");
1211 1212

        if (has_flagged)
Jens Georg's avatar
Jens Georg committed
1213
            toolbtn_flag.set_icon_name("filter-flagged-symbolic");
1214
        else
Jens Georg's avatar
Jens Georg committed
1215
            toolbtn_flag.set_icon_name("filter-flagged-disabled-symbolic");
1216 1217
    }
    
1218 1219
    // Ticket #3260 part IV - display the context menu on secondary click
    private bool on_context_menu_requested(int x, int y, int button) { 
1220
        close_menu.popup_at_pointer(null);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
        return false;
    }
    
    // Ticket #3260 part III - this runs whenever 'close'
    // is chosen in the context menu.
    private void on_context_menu_close_chosen() { 
        AppWindow aw = LibraryWindow.get_app();        
        
        // Try to obtain the action for toggling the searchbar.  If
        // it's null, then we're probably in direct edit mode, and 
        // shouldn't do anything anyway.
1232 1233
        var action = aw.lookup_action ("CommonDisplaySearchbar") as
            GLib.SimpleAction;
1234 1235
        
        // Could we find the appropriate action?
1236
        if(action != null) {
1237
            // Yes, hide the search bar.
1238
            action.change_state(false);
1239 1240 1241
        }
    }
    
1242
    private void on_flagged_toggled() {
1243 1244 1245
        update();
    }
    
1246
    private void on_videos_toggled() {
1247 1248 1249
        update();
    }
    
1250
    private void on_photos_toggled() {
1251 1252 1253
        update();
    }
    
1254
    private void on_raw_toggled() {
1255 1256 1257
        update();
    }
    
1258
    private void on_search_text_changed() {
1259 1260
        update();
    }
1261

1262
    private void on_rating_changed() {
1263 1264 1265 1266 1267
        AppWindow aw = LibraryWindow.get_app();

        if (aw == null)
            return;

1268 1269 1270 1271 1272
        var action = aw.lookup_action ("CommonDisplaySearchbar") as
            GLib.SimpleAction;

        // Could we find the appropriate action?
        if(action != null) {
1273
            action.change_state(true);
1274
        }
1275

1276
        update();
1277
    }
1278

1279 1280 1281 1282 1283 1284 1285
    // Ticket #3290, part II - listen for criteria change signals,
    // and show or hide widgets based on the criteria we just 
    // changed to.
    private void on_criteria_changed() {
        update();
    }
    
1286
    public void set_view_filter(SearchViewFilter search_filter) {
1287 1288 1289
        if (search_filter == this.search_filter)
            return;
        
1290 1291
        this.search_filter = search_filter;
        
1292
        // Enable/disable toolbar features depending on what the filter offers
1293
        actions.set_sensitive_for_search_criteria((SearchFilterCriteria) search_filter.get_criteria());
1294
        rating_button.sensitive = (SearchFilterCriteria.RATING & search_filter.get_criteria()) != 0;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
        
        update();
    }
    
    public void unset_view_filter() {
        set_view_filter(new DisabledViewFilter());
    }
    
    // Forces an update of the search filter.
    public void update() {
1305 1306 1307 1308 1309 1310
        if (null == search_filter) {
            // Search bar isn't being shown, need to toggle it.
            LibraryWindow.get_app().show_search_bar(true);
        }
        
        assert(null != search_filter);
1311
        
1312
        search_filter.set_search_filter(actions.text.value);
1313 1314 1315 1316 1317 1318 1319 1320
        search_filter.flagged = actions.flagged.get_state ().get_boolean ();
        search_filter.show_media_video = actions.videos.get_state
            ().get_boolean ();
        search_filter.show_media_photos = actions.photos.get_state
            ().get_boolean ();
        search_filter.show_media_raw = actions.raw.get_state ().get_boolean ();

        var filter = (RatingFilter) int.parse (actions.rating.get_state ().get_string ());
1321 1322
        search_filter.set_rating_filter(filter);
        rating_button.set_filter_icon(filter);
1323 1324

        search_filter.saved_search = saved_search;
1325
        
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
        // Ticket #3290, part III - check the current criteria
        // and show or hide widgets as needed.
        SearchFilterCriteria criteria = actions.get_criteria();
        
        search_box.visible = ((criteria & SearchFilterCriteria.TEXT) != 0);

        rating_button.visible = ((criteria & SearchFilterCriteria.RATING) != 0);
        
        toolbtn_flag.visible = ((criteria & SearchFilterCriteria.FLAG) != 0);
        
        label_type.visible = ((criteria & SearchFilterCriteria.MEDIA) != 0);
        toolbtn_photos.visible = ((criteria & SearchFilterCriteria.MEDIA) != 0); 
        toolbtn_videos.visible = ((criteria & SearchFilterCriteria.MEDIA) != 0);
        toolbtn_raw.visible = ((criteria & SearchFilterCriteria.MEDIA) != 0);

1341 1342
        saved_search_button.visible = ((criteria & SearchFilterCriteria.SAVEDSEARCH) != 0);

1343 1344
        // Ticket #3290, part IV - ensure that the separators
        // are shown and/or hidden as needed.
1345
        sepr_mediatype_flagged.visible = (label_type.visible && toolbtn_flag.visible);
1346

1347 1348
        sepr_flagged_rating.visible = ((label_type.visible && rating_button.visible) ||
            (toolbtn_flag.visible && rating_button.visible));
1349

1350
        // Send update to view collection.
1351 1352 1353
        search_filter.refresh();
    }
    
1354 1355 1356 1357 1358 1359 1360 1361 1362
    private void on_savedsearch_selected(SavedSearch saved_search) {
        this.saved_search = saved_search;
        update();
    }

    private void disable_savedsearch() {
        this.saved_search = null;
        update();
    }
1363

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    private void edit_dialog(SavedSearch search) {
        saved_search_button.filter_popup.hide();
        SavedSearchDialog ssd = new SavedSearchDialog.edit_existing(search);
        ssd.show();
    }

    private void delete_dialog(SavedSearch search) {
        saved_search_button.filter_popup.hide();
        if (Dialogs.confirm_delete_saved_search(search))
            AppWindow.get_command_manager().execute(new DeleteSavedSearchCommand(search));
    }

    private void add_dialog() {
        saved_search_button.filter_popup.hide();
        (new SavedSearchDialog()).show();
    }

    private void on_popover_closed() {
        // set_active emits clicked, so have a flag to not actually do anything
        elide_showing_again = true;
        saved_search_button.set_active(saved_search != null);
        saved_search_button.filter_popup.hide();
    }

    private void on_saved_search_button_clicked() {
        if (elide_showing_again && saved_search == null) {
        } else if (saved_search != null) {
            saved_search = null;
            saved_search_button.set_active(false);
            disable_savedsearch();
        } else {
            if (saved_search_button.filter_popup != null) {
                saved_search_button.filter_popup.edit_clicked.disconnect(edit_dialog);
                saved_search_button.filter_popup.search_activated.disconnect(on_savedsearch_selected);
                saved_search_button.filter_popup.delete_clicked.disconnect(delete_dialog);
                saved_search_button.filter_popup.add_clicked.disconnect(add_dialog);
                saved_search_button.filter_popup.closed.disconnect(on_popover_closed);
            }
            saved_search_button.filter_popup = new SavedSearchPopover(saved_search_button);
            saved_search_button.filter_popup.edit_clicked.connect(edit_dialog);
            saved_search_button.filter_popup.search_activated.connect(on_savedsearch_selected);
            saved_search_button.filter_popup.delete_clicked.connect(delete_dialog);
            saved_search_button.filter_popup.add_clicked.connect(add_dialog);
            saved_search_button.filter_popup.closed.connect(on_popover_closed);

            saved_search_button.filter_popup.show_all();
        }
        elide_showing_again = false;
    }

1414
    public void take_focus() {
1415 1416 1417
        search_box.get_focus();
    }
}