Project

General

Profile

unexist.dev

Assorted tidbits and projects

Snippets » History » Version 23

Anonymous, 10/17/2010 12:25 AM

1 23
h1. Snippets\015\012\015\012This page shows small snippets that might be useful:\015\012\015\012{{>toc}}\015\012\015\012h2. Alt-Tab\015\012\015\012This cycles through windows of view\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012grab "A-Tab" do |c|\015\012   sel     = 0\015\012   clients = Subtlext::View[:current].clients\015\012\015\012   clients.each_index do |idx|\015\012     if(clients[idx].id == c.id)\015\012       sel = idx + 1 if(idx < clients.size - 1)\015\012     end\015\012   end\015\012\015\012  clients[sel].focus\015\012end\015\012</code></pre>\015\012\015\012h2. Gravity tiling\015\012\015\012Tiles windows vertically that share a gravity.\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012on :tile do\015\012  clients = Subtlext::View.current.clients\015\012  screen  = Subtlext::Screen.current\015\012\015\012  assoc = {}\015\012\015\012  clients.each do |c|\015\012    begin\015\012      assoc[c.gravity.id] << c rescue assoc[c.gravity.id] = [ c ]\015\012    rescue StandardError\015\012      # Startup errors\015\012    end\015\012  end\015\012\015\012  assoc.each do |k, v|\015\012    g      = Subtlext::Gravity[k]\015\012    border = 2 #< Currently hardcoded\015\012\015\012    # Calculate gravity\015\012    x, y, w, h = g.geometry_for(screen)\015\012    width      = w / v.size\015\012    pos        = x\015\012\015\012    v.each do |c|\015\012      c.resize   = false\015\012      c.geometry = [ pos, y, width - 2 * border, h - 2 * border ]\015\012      pos += width\015\012    end\015\012  end\015\012end\015\012</code></pre>\015\012\015\012h2. Extend view\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012require "subtle/subtlext"\015\012\015\012STORE ||= {}\015\012\015\012module Subtlext\015\012  class View\015\012    def method_missing(meth, *args)\015\012      STORE[self.name] = { } unless(STORE.has_key?(self.name))\015\012\015\012      if(meth.to_s.end_with?("="))\015\012        meth = meth.to_s.chop.to_sym\015\012\015\012        STORE[self.name][meth] = args[0]\015\012      else\015\012        STORE[self.name][meth]\015\012      end\015\012    end\015\012  end\015\012end\015\012</code></pre>\015\012\015\012Finally make some use of this like following hook:\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012on :view_jump do |v|\015\012  v.visits += 1 rescue v.visits = 1\015\012  puts "View %s, %d visits" % [ v.name, v.visits ]\015\012end\015\012</code></pre>\015\012\015\012h2. Focus gravities\015\012\015\012Focus window a specific gravities on view.\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012{\015\012  "KP_7" => :top_left,    "KP_8" => :top,    "KP_9" => :top_right,\015\012  "KP_4" => :left,        "KP_5" => :center, "KP_6" => :right,\015\012  "KP_1" => :bottom_left, "KP_2" => :bottom, "KP_3" => :bottom_right\015\012}.each do |k, v|\015\012  grab "A-C-" + k, lambda {\015\012    c = Subtlext::View.current.clients.select { |c|\015\012      c.gravity.name.to_sym == v\015\012    }\015\012  \015\012    c.first.focus unless(c.empty?)\015\012  }\015\012end\015\012</code></pre>\015\012\015\012h2. Move windows\015\012\015\012This snippet adds nine [[grabs]] to move windows on the fly to nine defined views. It uses [[tagging]] for this, creates [[tags]] based on the view names and applies them when needed.\015\012\015\012<pre>{{hide}}<code class="ruby">\015\012(1..9).each do |i|\015\012  grab "C-%d" % [ i ] do |c|\015\012    views = Subtlext::View.all.map { |v| v.name }\015\012\015\012    # Adjust tags\015\012    if(i < views.size)\015\012      views.each do |v|\015\012        c.untag(v)\015\012      end\015\012\015\012      c.tag(views[i - 1])\015\012\015\012      Subtlext::View[views[i - 1]].tag(views[i - 1])\015\012    end\015\012  end\015\012end\015\012\015\012on :start do\015\012  # Create missing tags\015\012  views = Subtlext::View.all.map { |v| v.name }\015\012  tags  = Subtlext::Tag.all.map { |t| t.name }\015\012\015\012  views.each do |v|\015\012    unless(tags.include?(v))\015\012      t = Subtlext::Tag.new(v)\015\012      t.save\015\012    end\015\012  end\015\012end\015\012</code></pre>\015\012\015\012