| 1 | |
|---|
| 2 | |
|---|
| 3 | require 'osx/cocoa' |
|---|
| 4 | require 'uri' |
|---|
| 5 | require 'yaml' |
|---|
| 6 | include OSX |
|---|
| 7 | |
|---|
| 8 | class ConfigController < NSObject |
|---|
| 9 | ib_outlets :chkDontFadeout |
|---|
| 10 | ib_outlets :chkIgnoreMouseEvent |
|---|
| 11 | ib_outlets :chkStealthMode |
|---|
| 12 | ib_outlets :tblKeywords |
|---|
| 13 | ib_outlets :txtFont |
|---|
| 14 | ib_outlets :txtLineHeight |
|---|
| 15 | ib_outlets :txtWidth |
|---|
| 16 | ib_outlets :txtLines |
|---|
| 17 | ib_outlets :stpLineHeight |
|---|
| 18 | ib_outlets :stpWidth |
|---|
| 19 | ib_outlets :stpLines |
|---|
| 20 | ib_outlets :sldrMinAlphaValue, :sldrMaxAlphaValue |
|---|
| 21 | ib_outlets :window |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | ns_overrides "init" |
|---|
| 25 | |
|---|
| 26 | attr_accessor :keywords, :font, :config |
|---|
| 27 | attr_writer :logger |
|---|
| 28 | |
|---|
| 29 | # キーワードテーブル関連 |
|---|
| 30 | def numberOfRowsInTableView(table) |
|---|
| 31 | @keywords.length |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | def tableView_objectValueForTableColumn_row(table, column, row) |
|---|
| 35 | case column.identifier.to_s |
|---|
| 36 | when 'regexp' |
|---|
| 37 | @keywords[row][0] |
|---|
| 38 | when 'color' |
|---|
| 39 | @keywords[row][1] |
|---|
| 40 | when 'hilight' |
|---|
| 41 | @keywords[row][2] ? 1 : 0 |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 45 | def tableView_setObjectValue_forTableColumn_row(table, value, column, row) |
|---|
| 46 | case column.identifier.to_s |
|---|
| 47 | when 'regexp' |
|---|
| 48 | @keywords[row][0] = value.to_s |
|---|
| 49 | when 'color' |
|---|
| 50 | @keywords[row][1] = value.to_s |
|---|
| 51 | when 'hilight' |
|---|
| 52 | @keywords[row][2] = (value.to_i == 1) ? true : false |
|---|
| 53 | end |
|---|
| 54 | changeConfig(nil) |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row) |
|---|
| 58 | case column.identifier.to_s |
|---|
| 59 | when 'regexp' |
|---|
| 60 | when 'color' |
|---|
| 61 | _, r, g, b = */(..)(..)(..)$/.match(cell.stringValue.to_s).to_a.map{|i| i.to_i(16) / 255.0 } |
|---|
| 62 | color = NSColor.colorWithCalibratedRed_green_blue_alpha(r, g, b, 1) |
|---|
| 63 | cell.setDrawsBackground(true) |
|---|
| 64 | cell.setTextColor(color) |
|---|
| 65 | cell.setBackgroundColor(NSColor.blackColor) |
|---|
| 66 | when 'hilight' |
|---|
| 67 | end |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | # 設定 |
|---|
| 71 | def load_config(*) |
|---|
| 72 | @config = { |
|---|
| 73 | :uri => 'druby://:9876', |
|---|
| 74 | :min_alpha => 0, |
|---|
| 75 | :max_alpha => 1, |
|---|
| 76 | :fadein_speed => 0.05, |
|---|
| 77 | :fadeout_speed => 0.05, |
|---|
| 78 | :font_name => nil, |
|---|
| 79 | :font_size => 13, |
|---|
| 80 | :line_height => 1.3, |
|---|
| 81 | :fadeout_time => 10, |
|---|
| 82 | :ignore_mouse => true, |
|---|
| 83 | :dont_fadeout => false, |
|---|
| 84 | :width => 600, |
|---|
| 85 | :lines => 15, |
|---|
| 86 | :stealth => true, |
|---|
| 87 | :keys => { |
|---|
| 88 | :toggle_hide => 'Command+Shift+H', |
|---|
| 89 | :toggle_timeout => nil, |
|---|
| 90 | :ignore_mouse_event => nil, |
|---|
| 91 | }, |
|---|
| 92 | :keywords => [ |
|---|
| 93 | ] |
|---|
| 94 | } |
|---|
| 95 | @need_reload = %w(width lines uri font_name font_size) |
|---|
| 96 | begin |
|---|
| 97 | @config.update YAML.load_file("#{ENV['HOME']}/.iircv.yaml") |
|---|
| 98 | rescue Errno::ENOENT |
|---|
| 99 | end |
|---|
| 100 | log @config |
|---|
| 101 | @font = NSFont.fontWithName_size(@config[:font_name], @config[:font_size]) |
|---|
| 102 | @font = NSFont.boldSystemFontOfSize(@config[:font_size]) unless @font |
|---|
| 103 | @keywords = @config[:keywords] |
|---|
| 104 | @txtFont.setStringValue(@font.displayName) |
|---|
| 105 | |
|---|
| 106 | @chkDontFadeout.setIntValue(@config[:dont_fadeout]) |
|---|
| 107 | @chkIgnoreMouseEvent.setIntValue(@config[:ignore_mouse]) |
|---|
| 108 | @chkStealthMode.setIntValue(@config[:stealth]) |
|---|
| 109 | @txtLineHeight.setFloatValue(@config[:line_height]) |
|---|
| 110 | @txtWidth.setIntValue(@config[:width]) |
|---|
| 111 | @txtLines.setIntValue(@config[:lines]) |
|---|
| 112 | @sldrMinAlphaValue.setFloatValue(@config[:min_alpha]) |
|---|
| 113 | @sldrMaxAlphaValue.setFloatValue(@config[:max_alpha]) |
|---|
| 114 | |
|---|
| 115 | @stpLines.setIntValue(@txtLines.intValue) |
|---|
| 116 | @stpWidth.setIntValue(@txtWidth.intValue) |
|---|
| 117 | @stpLineHeight.setFloatValue(@txtLineHeight.floatValue) |
|---|
| 118 | end |
|---|
| 119 | |
|---|
| 120 | def save_config |
|---|
| 121 | File.open("#{ENV['HOME']}/.iircv.yaml", "w") do |f| |
|---|
| 122 | YAML.dump(@config, f) |
|---|
| 123 | end |
|---|
| 124 | end |
|---|
| 125 | |
|---|
| 126 | def setDelegate(cont) |
|---|
| 127 | @appController = cont |
|---|
| 128 | end |
|---|
| 129 | |
|---|
| 130 | # actions |
|---|
| 131 | def addKeyword(sender) |
|---|
| 132 | @keywords << ['new keyword', '#ffcc00', false] |
|---|
| 133 | @tblKeywords.reloadData |
|---|
| 134 | end |
|---|
| 135 | |
|---|
| 136 | def deleteKeyword(sender) |
|---|
| 137 | @keywords.delete_at @tblKeywords.selectedRow |
|---|
| 138 | @tblKeywords.reloadData |
|---|
| 139 | end |
|---|
| 140 | |
|---|
| 141 | def changeConfig(sender) |
|---|
| 142 | # reset configs |
|---|
| 143 | @config[:stealth] = @chkStealthMode.intValue? |
|---|
| 144 | @config[:dont_fadeout] = @chkDontFadeout.intValue? |
|---|
| 145 | @config[:ignore_mouse_event] = @chkIgnoreMouseEvent.intValue? |
|---|
| 146 | @config[:keywords] = @keywords |
|---|
| 147 | @config[:lines] = @txtLines.intValue |
|---|
| 148 | @config[:line_height] = @txtLineHeight.floatValue |
|---|
| 149 | @config[:font_name] = @font.fontName.to_s |
|---|
| 150 | @config[:font_size] = @font.pointSize |
|---|
| 151 | @config[:width] = @txtWidth.intValue |
|---|
| 152 | save_config |
|---|
| 153 | end |
|---|
| 154 | |
|---|
| 155 | def changeMaxAlphaValue(sender) |
|---|
| 156 | @config[:max_alpha] = sender.floatValue |
|---|
| 157 | @appController.window.setAlphaValue(sender.floatValue) |
|---|
| 158 | end |
|---|
| 159 | |
|---|
| 160 | def changeMinAlphaValue(sender) |
|---|
| 161 | @config[:min_alpha] = sender.floatValue |
|---|
| 162 | @appController.window.setAlphaValue(sender.floatValue) |
|---|
| 163 | end |
|---|
| 164 | |
|---|
| 165 | def selectFont(sender) |
|---|
| 166 | fontPanel = NSFontManager.sharedFontManager.fontPanel(true) |
|---|
| 167 | fontPanel.setPanelFont_isMultiple(@font, false) |
|---|
| 168 | fontPanel.makeKeyAndOrderFront(self) |
|---|
| 169 | end |
|---|
| 170 | |
|---|
| 171 | def changeFont(manager) |
|---|
| 172 | @font = manager.convertFont(@font) |
|---|
| 173 | @txtFont.setStringValue(@font.displayName) |
|---|
| 174 | changeConfig(nil) |
|---|
| 175 | end |
|---|
| 176 | |
|---|
| 177 | def relunch(*) |
|---|
| 178 | path = NSBundle.mainBundle.executablePath.to_s |
|---|
| 179 | fork do |
|---|
| 180 | sleep 2 |
|---|
| 181 | exec(path) |
|---|
| 182 | end |
|---|
| 183 | OSX::NSApp.stop(nil) |
|---|
| 184 | end |
|---|
| 185 | |
|---|
| 186 | def show(sender) |
|---|
| 187 | @prev_config = @config.dup |
|---|
| 188 | @window.makeKeyAndOrderFront(nil) |
|---|
| 189 | end |
|---|
| 190 | |
|---|
| 191 | def windowShouldClose(sender) |
|---|
| 192 | changeConfig(nil) |
|---|
| 193 | if @need_reload.any? {|i| @config[i.to_sym] != @prev_config[i.to_sym] } |
|---|
| 194 | case NSRunAlertPanel(_("Reload Configure"), _("Restart Program to Apply Some Settings?"), _("Yes"), _("No"), nil) |
|---|
| 195 | when 1 # yes |
|---|
| 196 | relunch |
|---|
| 197 | when 0 # no |
|---|
| 198 | end |
|---|
| 199 | end |
|---|
| 200 | @appController.set_view_props |
|---|
| 201 | @logger.info _("Applied Settings.") |
|---|
| 202 | @window.orderOut(nil) |
|---|
| 203 | false |
|---|
| 204 | end |
|---|
| 205 | |
|---|
| 206 | def awakeFromNib |
|---|
| 207 | end |
|---|
| 208 | |
|---|
| 209 | def init |
|---|
| 210 | super_init |
|---|
| 211 | @keywords = [] |
|---|
| 212 | self |
|---|
| 213 | end |
|---|
| 214 | end |
|---|