Changeset 747 for lang/ruby

Show
Ignore:
Timestamp:
10/27/07 11:39:32 (6 years ago)
Author:
cho45
Message:

lang/ruby/Amalgam/AppController.rb,
lang/ruby/Amalgam/Amalgam.xcodeproj/cho45.mode1,
lang/ruby/Amalgam/English.lproj/MainMenu.nib/objects.nib,
lang/ruby/Amalgam/English.lproj/MainMenu.nib/info.nib,
lang/ruby/Amalgam/English.lproj/MainMenu.nib/classes.nib:

いろいろと実装変更
コンボボックスやめた。

Location:
lang/ruby/Amalgam
Files:
2 added
1 removed
5 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/Amalgam

    • Property svn:ignore set to
      build
      pkg
  • lang/ruby/Amalgam/AppController.rb

    r746 r747  
    2929end 
    3030 
     31class Candidate 
     32        attr_accessor :name, :icon, :proc 
     33 
     34        def initialize(name, icon=nil, &block) 
     35                @name = name 
     36                @proc = block 
     37                case icon 
     38                when Symbol 
     39                        @icon = NSWorkspace.sharedWorkspace.iconForFileType(icon.to_s) 
     40                when Pathname 
     41                        @icon = NSWorkspace.sharedWorkspace.iconForFile(icon.to_s) 
     42                when NilClass 
     43                        @icon = NSWorkspace.sharedWorkspace.iconForFileType("unknown") 
     44                else 
     45                        str = NSData.objc_send :dataWithBytes, icon.to_s, :length 
     46                        @icon = NSImage.alloc.initWithData(str) 
     47                end 
     48        end 
     49 
     50        def call(*args) 
     51                if @proc.arity.abs > 0 
     52                        @proc.call(self, *args) 
     53                end 
     54        end 
     55end 
     56 
    3157class AppController < NSObject 
    32         ib_outlets :view 
    33         ib_outlets :combo 
    34  
    35         def comboBox_objectValueForItemAtIndex(sender, index) 
    36                 (@candidate || [])[index].first 
    37         end 
    38  
    39         def numberOfItemsInComboBox(sender) 
    40                 # log "Reloaded data" 
    41                 (@candidate || []).length 
    42         end 
    43  
    44         def comboBox_completedString(sender, string) 
    45                 #log "completedString->#{string}" 
    46                 string = Regexp.escape(string) 
    47                 (@candidate || []).transpose[0].grep(/^#{string}/i).first 
    48         rescue Exception 
    49                 "" 
    50         end 
    51  
    52         def comboBox_indexOfItemWithStringValue(sender, string) 
    53                 @candidate ||= [] 
    54                 @candidate.each_with_index do |(r,l),i| 
    55                         if r === string 
    56                                 return i 
    57                         end 
    58                 end 
    59                 NSNotFound 
    60         end 
    61  
    62 #       def controlTextDidChange(anot) 
    63 #       end 
     58        ib_outlet :view 
     59        ib_outlet :input 
     60        ib_outlet :results 
     61 
     62        # Tableview 
     63        def numberOfRowsInTableView(table) 
     64                @now.length 
     65        end 
     66 
     67        def tableView_objectValueForTableColumn_row(table, column, row) 
     68                case column.identifier.to_s.to_sym 
     69                when :icon 
     70                        @now[row].icon ||  
     71                                NSWorkspace.sharedWorkspace.iconForFileType('unknown') 
     72                when :name 
     73                        @now[row].name 
     74                end 
     75        end 
     76 
     77        def tableView_setObjectValue_forTableColumn_row(table, value, column, row) 
     78        end 
     79 
     80        def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row) 
     81        end 
     82 
     83        def run_current_selection(*) 
     84                if @now[@results.selectedRow] 
     85                        @now[@results.selectedRow].call 
     86                end 
     87        end 
     88 
     89        def controlTextDidChange(anot) 
     90                filtering @input.stringValue 
     91                @results.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(0), false) 
     92        end 
    6493 
    6594        def windowDidResignKey(anot) 
     
    72101 
    73102        def windowDidBecomeMain(anot) 
    74                 @window.makeFirstResponder(@combo) 
    75                 @combo.delegate = self 
    76                 #@combo.reloadData 
    77                 @editting = false 
    78                 #@combo.stringValue = "" 
     103                @window.makeFirstResponder(@input) 
    79104        end 
    80105 
    81106        def windowDidBecomeKey(anot) 
    82                 @window.makeFirstResponder(@combo) 
     107                @window.makeFirstResponder(@input) 
     108                @now = @candidates 
    83109        end 
    84110 
     
    105131                @window.setFrameOrigin([screenFrame.size.width / 2, screenFrame.size.height / 2]) 
    106132 
    107                 @combo.editable = true 
    108                 @combo.numberOfVisibleItems = 20 
    109  
    110133                NSApp.register_hotkey("Command+`") do 
    111134                        log "HotKey hitted" 
     
    117140 
    118141                build_candidates() 
     142                @results.dataSource = self 
     143 
    119144 
    120145                NSApp.activateIgnoringOtherApps(true) 
    121                 @window.makeFirstResponder(@combo) 
     146                @window.makeFirstResponder(@input) 
     147        end 
     148 
     149        def filtering(str) 
     150                str = str.to_s 
     151                if str =~ /[A-Z]/ 
     152                        r = /^#{str}/ 
     153                else 
     154                        r = /^#{str}/i 
     155                end 
     156                @now = @candidates.select {|i| 
     157                        r === i.name 
     158                }.sort_by {|i| i.name.length } 
     159 
     160                if @now.length.zero? 
     161                        r = /#{str.split(//).map {|c| Regexp.escape(c) }.join(".*")}/i 
     162                        @now = @candidates.select {|i| 
     163                                r === i.name 
     164                        }.sort_by {|i| 
     165                                r.match(i.name).begin(0) 
     166                        } 
     167                        @results.usesAlternatingRowBackgroundColors = false 
     168                        @results.backgroundColor = NSColor.objc_send( 
     169                                :colorWithCalibratedRed, 0.95, 
     170                                :green, 0.90, 
     171                                :blue, 0.90, 
     172                                :alpha, 1 
     173                        ) 
     174                else 
     175                        @results.usesAlternatingRowBackgroundColors = true 
     176                end 
     177 
     178                @results.reloadData 
     179        end 
     180 
     181        def nextCandidate(sender) 
     182                if @results.selectedRow <= @now.size 
     183                        @results.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@results.selectedRow+1), false) 
     184                        @results.scrollRowToVisible(@results.selectedRow) 
     185                end 
     186        end 
     187 
     188        def prevCandidate(sender) 
     189                if @results.selectedRow > 0 
     190                        @results.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@results.selectedRow-1), false) 
     191                        @results.scrollRowToVisible(@results.selectedRow) 
     192                end 
    122193        end 
    123194 
    124195        def process_keyevent(e) 
    125                 unless @editting 
    126                         @combo.stringValue = "" 
    127                         @editting = true 
    128                         @combo.textColor = NSColor.controlTextColor 
    129                 end 
    130                 @combo.scrollItemAtIndexToTop(0) 
    131196                key = key_string(e) 
    132197                #log key 
    133198                case key 
     199                when "C-n" 
     200                        nextCandidate nil 
     201                when "C-p" 
     202                        prevCandidate nil 
    134203                when "\r" 
    135                         unless @combo.stringValue.strip.empty? 
    136                                 index = comboBox_indexOfItemWithStringValue(@combo, @combo.stringValue) 
    137                                 unless index == NSNotFound 
    138                                         pro = @candidate[index][1] 
    139                                         if pro.arity == 0 
    140                                                 pro.call 
    141                                         else 
    142                                                 pro.call(@candidate[index][0], @combo.stringValue.to_s) 
    143                                         end 
    144                                 end 
     204                        log @input.stringValue 
     205                        unless @input.stringValue.strip.empty? 
     206                                run_current_selection 
    145207                        end 
    146208                        @window.orderOut(nil) 
     209                when "\t" 
     210                when "S-\t" 
    147211                when "C-u" 
    148                         @combo.stringValue = "" 
     212                        @input.stringValue = "" 
     213                        filtering "" 
    149214                when "C-c" 
    150215                        @window.orderOut(nil) 
    151216                end 
     217 
    152218                false 
    153219        end 
     
    175241 
    176242        def build_candidates 
    177                 @candidate = [] 
    178  
    179                 @applications = [Pathname.new("Finder")] 
     243                @candidates = [] 
     244 
     245                @applications = [] 
    180246                search_applications_recursive("/Applications") 
    181247                search_applications_recursive("/Developer/Applications") 
    182248 
    183                 @candidate = @applications.map {|f| 
    184                         [f.basename(".app").to_s, proc { 
     249                @candidates = @applications.map {|f| 
     250                        Candidate.new(f.basename(".app").to_s, f.realpath) do 
    185251                                system("open", "-a", f) 
    186                         }] 
     252                        end 
    187253                } 
    188254 
    189                 @candidate.concat Pathname.glob("/System/Library/PreferencePanes/*.prefPane").map {|f| 
    190                         [f.basename.to_s, proc { 
     255                @candidates.concat Pathname.glob("/System/Library/PreferencePanes/*.prefPane").map {|f| 
     256                        Candidate.new(f.basename.to_s, f.realpath) do 
    191257                                system("open", f) 
    192                         }] 
     258                        end 
    193259                } 
    194260 
    195                 @candidate << [/.d (.+)/, proc {|s,m| 
    196                         code = m[s, 1] 
    197                         result = "" 
    198                         begin 
    199                                 result = eval(code, binding).inspect 
    200                         rescue Exception => e 
    201                                 result = e.inspect 
    202                         end 
    203                         log result 
    204                         OSX.NSRunAlertPanel("Debug", result, "Ok", "", nil) 
    205                 }] 
    206  
    207                 @candidate << ["Exit", proc { 
     261                #       TODO 
     262#               @candidates << Candidate.new(/.d (.+)/, NSWorkspace.sharedWorkspace.iconForFile("sh")) do 
     263#                       code = m[s, 1] 
     264#                       result = "" 
     265#                       begin 
     266#                               result = eval(code, binding).inspect 
     267#                       rescue Exception => e 
     268#                               result = e.inspect 
     269#                       end 
     270#                       log result 
     271#                       OSX.NSRunAlertPanel("Debug", result, "Ok", "", nil) 
     272#               end 
     273 
     274                @candidates << Candidate.new("Exit", :sh) do 
    208275                        if OSX.NSRunAlertPanel("Sure?", "exit ok?", "Exit", "Cancel", nil) == NSAlertDefaultReturn 
    209276                                NSApp.terminate(nil) 
    210277                        end 
    211                 }] 
    212  
     278                end 
     279 
     280                @now = @candidates 
    213281                log "Search done" 
    214282                #set_status "Search done" 
  • lang/ruby/Amalgam/English.lproj

    • Property svn:ignore set to
      MainMenu~.nib
  • lang/ruby/Amalgam/English.lproj/MainMenu.nib/classes.nib

    r734 r747  
    44            CLASS = AppController;  
    55            LANGUAGE = ObjC;  
    6             OUTLETS = {combo = id; view = id; };  
     6            OUTLETS = {input = id; results = id; view = id; };  
    77            SUPERCLASS = NSObject;  
    88        },  
  • lang/ruby/Amalgam/English.lproj/MainMenu.nib/info.nib

    r741 r747  
    88        <dict> 
    99                <key>196</key> 
    10                 <string>538 496 358 63 0 0 1280 778 </string> 
     10                <string>350 466 358 235 0 0 1280 778 </string> 
    1111        </dict> 
    1212        <key>IBFramework Version</key>