Advertisement
rotrevrep

Revealer Button with direction

Sep 1st, 2015
3,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 4.36 KB | None | 0 0
  1. public class RevealerBox : Gtk.Box {
  2.     public class RevealerBox (Gtk.DirectionType direction) {
  3.         Object (direction: direction);
  4.     }
  5.    
  6.     Gtk.Revealer revealer;
  7.     Gtk.Image image;
  8.    
  9.     construct {
  10.         orientation = (direction == Gtk.DirectionType.UP || direction == Gtk.DirectionType.DOWN) ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL;
  11.        
  12.         image = new Gtk.Image.from_icon_name ("go-next", Gtk.IconSize.BUTTON);
  13.         if (direction == Gtk.DirectionType.UP)
  14.             image.icon_name = "go-up";
  15.         if (direction == Gtk.DirectionType.DOWN)
  16.             image.icon_name = "go-down";
  17.         if (direction == Gtk.DirectionType.LEFT)
  18.             image.icon_name = "go-previous";
  19.            
  20.         var button = new Gtk.Button();
  21.         button.clicked.connect (() => {
  22.             reveal_child = !reveal_child;
  23.         });
  24.         button.child = image;
  25.        
  26.         revealer = new Gtk.Revealer();
  27.        
  28.         if (direction == Gtk.DirectionType.RIGHT || direction == Gtk.DirectionType.DOWN) {
  29.             pack_start (button, false);
  30.             pack_start (revealer);
  31.         } else {
  32.             pack_start (revealer);
  33.             pack_start (button, false);
  34.         }
  35.     }
  36.    
  37.     void resize_window (bool shown) {
  38.         if (!resize_toplevel)
  39.             return;
  40.         var top = get_toplevel();
  41.         if (!top.get_realized() || !revealer.get_realized())
  42.             return;
  43.         Gtk.Allocation top_allocation;
  44.         Gtk.Allocation child_allocation;
  45.         top.get_allocation (out top_allocation);
  46.         revealer.get_child().get_allocation (out child_allocation);
  47.         if (direction == Gtk.DirectionType.UP || direction == Gtk.DirectionType.DOWN) {
  48.             if (shown) {
  49.                 int i;
  50.                 revealer.get_child().get_preferred_height_for_width (child_allocation.width, out i, null);
  51.                 top_allocation.height += i;
  52.             }
  53.             else
  54.                 top_allocation.height -= child_allocation.height;
  55.         }
  56.         else {
  57.             if (shown) {
  58.                 int i;
  59.                 revealer.get_child().get_preferred_width_for_height (child_allocation.height, out i, null);
  60.                 top_allocation.width += i;
  61.             }
  62.             else
  63.                 top_allocation.width -= child_allocation.width;
  64.         }
  65.         (top as Gtk.Window).resize (top_allocation.width, top_allocation.height);
  66.     }
  67.    
  68.     public new Gtk.Widget child {
  69.         owned get {
  70.             return revealer.get_child();
  71.         }
  72.         set {
  73.             revealer.child = value;
  74.         }
  75.     }
  76.    
  77.     public bool child_revealed {
  78.         get {
  79.             return revealer.child_revealed;
  80.         }
  81.     }
  82.    
  83.     public Gtk.DirectionType direction { get; construct; }
  84.    
  85.     public bool resize_toplevel { get; set; }
  86.    
  87.     public bool reveal_child {
  88.         get {
  89.             return revealer.reveal_child;
  90.         }
  91.         set {
  92.             if (value && direction == Gtk.DirectionType.RIGHT) {
  93.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_RIGHT;
  94.                 image.icon_name = "go-previous";
  95.             }
  96.             if (value && direction == Gtk.DirectionType.LEFT) {
  97.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;
  98.                 image.icon_name = "go-next";
  99.             }
  100.             if (value && direction == Gtk.DirectionType.UP) {
  101.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_UP;
  102.                 image.icon_name = "go-down";
  103.             }
  104.             if (value && direction == Gtk.DirectionType.DOWN) {
  105.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_DOWN;
  106.                 image.icon_name = "go-up";
  107.             }
  108.             if (!value && direction == Gtk.DirectionType.RIGHT) {
  109.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;
  110.                 image.icon_name = "go-next";
  111.             }
  112.             if (!value && direction == Gtk.DirectionType.LEFT) {
  113.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_RIGHT;
  114.                 image.icon_name = "go-previous";
  115.             }
  116.             if (!value && direction == Gtk.DirectionType.UP) {
  117.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_DOWN;
  118.                 image.icon_name = "go-up";
  119.             }
  120.             if (!value && direction == Gtk.DirectionType.DOWN) {
  121.                 revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_UP;
  122.                 image.icon_name = "go-down";
  123.             }
  124.             revealer.reveal_child = value;
  125.             revealer.set_child_visible (value);
  126.             queue_resize();
  127.             resize_window (value);
  128.         }
  129.     }
  130.    
  131.     public uint transition_duration {
  132.         get {
  133.             return revealer.transition_duration;
  134.         }
  135.         set {
  136.             revealer.transition_duration = value;
  137.         }
  138.     }
  139. }
  140.  
  141. public static void main (string[] args) {
  142.     Gtk.init (ref args);
  143.     var win = new Gtk.Window();
  144.     var revealer = new RevealerBox (Gtk.DirectionType.DOWN);
  145.     revealer.resize_toplevel = true;
  146.     revealer.transition_duration = 800;
  147.     revealer.child = new Gtk.Label ("toto");
  148.     revealer.reveal_child = true;
  149.     win.add (revealer);
  150.     win.show_all();
  151.     Gtk.main();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement