Skip to content
This repository
Browse code

playlist example

  • Loading branch information...
commit acb02967b341d67d17e3fc5f26bde4e2328dbe65 1 parent 67d1b9d
j. kaiden authored

Showing 1 changed file with 125 additions and 0 deletions. Show diff stats Hide diff stats

  1. +125 0 playlist_example.rb
125 playlist_example.rb
... ... @@ -0,0 +1,125 @@
  1 +require 'observer'
  2 +
  3 +class ListManager < Shoes::Widget
  4 + include Observable
  5 + def initialize(array)
  6 + @selected = []
  7 + self.show(array)
  8 + keypress{|k|
  9 + case k.to_s.downcase
  10 + when 'up'
  11 + @selected.each{|entry|
  12 + i = array.index(entry)
  13 + array.delete_at(i)
  14 + i -= 1
  15 + array.insert(i, entry)
  16 + }
  17 + @s.clear
  18 + self.show(array)
  19 + when 'down'
  20 + @selected.each{|entry|
  21 + i = array.index(entry)
  22 + array.delete_at(i)
  23 + i += 1
  24 + i = 0 if i > array.length
  25 + array.insert(i, entry)
  26 + }
  27 + @s.clear
  28 + self.show(array)
  29 + end
  30 + }
  31 + end
  32 +
  33 + def show(array)
  34 + @s = stack{
  35 + array.each{|item|
  36 + if @selected.include?(item)
  37 + cell = link(File.basename(item), stroke: yellow, underline: "none")
  38 + else
  39 + cell = link(File.basename(item), stroke: gray, underline: "none")
  40 + end
  41 + cell.hover{cell.style(fill: blue)}
  42 + cell.leave{cell.style(fill: black)}
  43 + cell.click{
  44 + if @selected.include?(item)
  45 + cell.style(stroke: gray)
  46 + changed
  47 + notify_observers("unselect:#{item}")
  48 + else
  49 + @selected << item
  50 + cell.style(stroke: yellow)
  51 + changed
  52 + notify_observers(item)
  53 + end
  54 + }
  55 + para cell
  56 + }
  57 + }
  58 + end
  59 +end #class ListManager
  60 +
  61 +class PlayList < Shoes::Widget
  62 + include Observable
  63 + def initialize(h)
  64 + @selected = []
  65 + th = (h * 0.9).round.to_i
  66 + bh = (h * 0.1).round.to_i
  67 + @top = stack width: 1.0, height: th, scroll: true
  68 + bottom = stack width: 1.0, height: bh, stroke: gray
  69 +
  70 + bottom.append{
  71 + pb = button("play"){
  72 + changed
  73 + notify_observers("PL:play:#{@selected}")
  74 + }
  75 + pb.style(top: 10)
  76 + }
  77 + end
  78 +
  79 + def add(list)
  80 + @top.append{
  81 + lm = list_manager(list)
  82 + lm.add_observer(self)
  83 + }
  84 + end
  85 +
  86 + def update(message)
  87 + if message.include?("unselect")
  88 + index = message.split(":")[-1]
  89 + @selected.delete(index)
  90 + else
  91 + @selected << message
  92 + end
  93 + end
  94 +end #class PlayList
  95 +
  96 +
  97 +Shoes.app title: "main" do
  98 +
  99 + r = (1..25)
  100 + @list = []
  101 + r.each{|n| @list << "entry ##{n}"}
  102 +
  103 + def update(message)
  104 + case
  105 + when message.include?("PL:play")
  106 + selected = message.split(":")[-1]
  107 + play(selected)
  108 + end
  109 + end
  110 +
  111 + def playlist(parent, list)
  112 + window do
  113 + pl = play_list(self.height)
  114 + pl.add(list)
  115 + pl.add_observer(parent)
  116 + end
  117 + end
  118 +
  119 + def play(selected)
  120 + para "play: #{selected}", stroke: gray
  121 + end
  122 +
  123 + button("playlist"){playlist(self, @list)}
  124 +
  125 +end #Shoes.app

0 comments on commit acb0296

Please sign in to comment.
Something went wrong with that request. Please try again.