Advertisement
rotrevrep

custom gtk windows

May 27th, 2013
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 5.33 KB | None | 0 0
  1. using Gtk;
  2.  
  3. namespace My
  4. {
  5.     public class Window : Gtk.Window
  6.     {
  7.         public uint delay{get;set;}
  8.         public Gdk.RGBA background_color{
  9.             set{
  10.                 override_background_color(StateFlags.NORMAL,value);
  11.             }
  12.         }
  13.        
  14.         public Window(WindowType wtype = WindowType.TOPLEVEL){
  15.             GLib.Object(type: wtype);
  16.             Timeout.add(100,()=>{
  17.                 int x; int y;
  18.                 Gdk.Display.get_default().get_device_manager().get_client_pointer().get_position(null,out x, out y);
  19.                 x_pointer = x;
  20.                 y_pointer = y;
  21.                 return true;
  22.             });
  23.         }
  24.        
  25.         public int y_pointer{get;private set;}
  26.         public int x_pointer{get;private set;}
  27.     }
  28.        
  29.     public class FadeWindow : My.Window
  30.     {
  31.         double fade = 1;
  32.        
  33.         public signal bool fade_completed();
  34.        
  35.         public FadeWindow(Gtk.WindowType window_type = Gtk.WindowType.TOPLEVEL){
  36.             base(window_type);
  37.             fade_range = 0.1;
  38.             delay = 10;
  39.         }
  40.        
  41.         public double fade_range{get;set;}
  42.        
  43.         public void fade_out(){
  44.             Timeout.add(delay,()=>{
  45.                 (this as Widget).set_opacity(fade);
  46.                  fade -= fade_range;
  47.                  if(fade < 0){fade_completed();return false;}
  48.                  return true;
  49.             });
  50.         }
  51.        
  52.         public void fade_in(){
  53.             Timeout.add(delay,()=>{
  54.                 (this as Widget).set_opacity(fade);
  55.                 fade += fade_range;
  56.                 if(fade > 1){fade_completed();return false;}
  57.                 return true;
  58.             });
  59.         }
  60.     }
  61.  
  62.     public enum WindowPosition{Left,Top,Right,Bottom}
  63.    
  64.     public class SlideWindow : My.Window
  65.     {  
  66.         double fade = 0;
  67.         double fade_range = 0.04;
  68.        
  69.         public SlideWindow(My.WindowPosition window_position = My.WindowPosition.Left, int size = 50){
  70.             base(WindowType.POPUP);
  71.             move_range = 2;
  72.             delay = 10;
  73.             position = window_position;
  74.             width = size;
  75.             switch(position){
  76.                 case My.WindowPosition.Top:
  77.                 move(0,-width);
  78.                 set_size_request(Gdk.Screen.width(),width);
  79.                 break;
  80.                 case My.WindowPosition.Right:
  81.                 move(Gdk.Screen.width()+width,0);
  82.                 set_size_request(width,Gdk.Screen.height());
  83.                 break;
  84.                 case My.WindowPosition.Bottom:
  85.                 move(0,Gdk.Screen.height()+width);
  86.                 set_size_request(Gdk.Screen.width(),width);
  87.                 break;
  88.                 default:
  89.                 move(-width,0);
  90.                 set_size_request(width,Gdk.Screen.height());
  91.                 break;
  92.             }
  93.             Timeout.add(100,()=>{
  94.                 int w = get_allocated_width();
  95.                 int h = get_allocated_height();
  96.                 switch(position){
  97.                     case My.WindowPosition.Top:
  98.                     if(y_pointer<2)move_in();
  99.                         else if(y_pointer>width)move_out();
  100.                     break;
  101.                     case My.WindowPosition.Right:
  102.                     if(x_pointer>Gdk.Screen.width()-2)move_in();
  103.                         else if(x_pointer<Gdk.Screen.width()-width)move_out();
  104.                     break;
  105.                     case My.WindowPosition.Bottom:
  106.                     if(y_pointer>Gdk.Screen.height()-2)move_in();
  107.                         else if(y_pointer<Gdk.Screen.height()-width)move_out();
  108.                     break;
  109.                     case My.WindowPosition.Left:
  110.                     if(x_pointer<2)
  111.                         move_in();
  112.                     else if(x_pointer>width)move_out();
  113.                     break;
  114.                 }
  115.                 return true;
  116.             });
  117.         }
  118.        
  119.         public signal bool move_completed();
  120.         public signal void move_init();
  121.        
  122.         public My.WindowPosition position{get;set;}
  123.         public int move_range{get;set;}
  124.         public int width{get;set;}
  125.        
  126.         public bool move_in(){
  127.             move_init();
  128.             int x; int y;
  129.             get_position(out x, out y);
  130.             int w = get_allocated_width();
  131.             int h = get_allocated_height();
  132.             Timeout.add(delay,()=>{
  133.                 switch(position){
  134.                     case My.WindowPosition.Top:
  135.                         if(y == 0){move_completed();return false;}
  136.                         y += move_range;
  137.                         move(x,y);
  138.                         (this as Widget).set_opacity((y+width)/((double)width));
  139.                     break;
  140.                     case My.WindowPosition.Right:
  141.                         if(x <= Gdk.Screen.width()-w){move_completed();return false;}
  142.                         x -= move_range;
  143.                         move(x,y);
  144.                         (this as Widget).set_opacity((Gdk.Screen.width()-x)/((double)width));
  145.                     break;
  146.                     case My.WindowPosition.Bottom:
  147.                         if(y <= Gdk.Screen.height()-h){move_completed();return false;}
  148.                         y -= move_range;
  149.                         move(x,y);
  150.                         (this as Widget).set_opacity((Gdk.Screen.height()-y)/((double)width));
  151.                     break;
  152.                     case My.WindowPosition.Left:       
  153.                         if(x == 0){move_completed();return false;}
  154.                         x += move_range;   
  155.                         move(x,y); 
  156.                         (this as Widget).set_opacity((width+x)/((double)width));
  157.                     break;
  158.                 }
  159.                 return true;
  160.             });
  161.             return true;
  162.         }
  163.        
  164.         public bool move_out(){
  165.             move_init();
  166.                 int x; int y;
  167.             get_position(out x, out y);
  168.             int w = get_allocated_width();
  169.             int h = get_allocated_height();
  170.             Timeout.add(delay,()=>{
  171.                 switch(position){
  172.                     case My.WindowPosition.Top:
  173.                         if(y <= -h){move_completed();return false;}
  174.                         y -= move_range;
  175.                         move(x,y);
  176.                         (this as Widget).set_opacity((width+y)/((double)width));
  177.                     break;
  178.                     case My.WindowPosition.Right:
  179.                         if(x > Gdk.Screen.width()){move_completed();return false;}
  180.                         x += move_range;
  181.                         move(x,y);
  182.                         (this as Widget).set_opacity((Gdk.Screen.width()-x)/((double)width));
  183.                     break;
  184.                     case My.WindowPosition.Bottom:
  185.                         if(y > Gdk.Screen.height()){move_completed();return false;}
  186.                         y += move_range;
  187.                         move(x,y);
  188.                         (this as Widget).set_opacity((Gdk.Screen.height()-y)/((double)width));
  189.                     break;
  190.                     case My.WindowPosition.Left:
  191.                         if(x <= -w){return false;}
  192.                         x -= move_range;
  193.                         move(x,y);
  194.                         (this as Widget).set_opacity((width+x)/((double)width));
  195.                     break;
  196.                 }
  197.                 return true;
  198.             });
  199.             return true;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement