Caged / gitnub

A Gitk-like application written in RubyCocoa that looks like it belongs on a Mac. See the wiki for downloads and screenshots.

This URL has Read+Write access

gitnub / controllers / ApplicationController.rb
4e75b7ba » Caged 2008-03-01 Initial import 1 #
2 # ApplicationController.rb
3 # GitNub
4 #
5 # Created by Justin Palmer on 3/1/08.
6 # Copyright (c) 2008 Active Reload, LLC. All rights reserved.
7 #
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 8 $VERBOSE = nil
4e75b7ba » Caged 2008-03-01 Initial import 9 require 'rubygems'
cd89473f » kballard 2008-03-21 Make nub (or open -a GitNub... 10 require 'pathname'
f74a0f36 » Caged 2008-03-03 Don't cause the task to wai... 11 require 'osx/cocoa'
dcacd139 » kballard 2008-05-10 Push the bundled lib dirs a... 12 libdir = OSX::NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("lib").fileSystemRepresentation
1c29dfa5 » Caged 2008-08-05 Fixes #19 LSOpenURLFromSpec... 13 $:.unshift(libdir, "#{libdir}/grit/lib", "#{libdir}/mime-types/lib", "#{libdir}/open4/lib")
00a2e576 » kballard 2008-05-10 Fix includes such that the ... 14 require 'grit'
15 require 'time_extensions'
72ace73a » Caged 2008-05-14 Keeping filename convention... 16 require 'string_extensions'
74cd84c5 » Caged 2008-06-13 Toggle controls when swappi... 17 require 'osx_notify'
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 18 require 'InfoWindowController'
4e75b7ba » Caged 2008-03-01 Initial import 19
88e97175 » benstiglitz 2008-03-12 Cleaned up the image and te... 20 OSX.ns_import 'CommitSummaryCell'
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 21 include OSX
22
cd89473f » kballard 2008-03-21 Make nub (or open -a GitNub... 23 # we use ENV['PWD'] instead of Dir.getwd if it exists so
24 # `open GitNub` will work, since that launches us at / but leaves ENV['PWD'] intact
25 pwd = Pathname.new(ENV['PWD'].nil? ? Dir.getwd : ENV['PWD'])
26 REPOSITORY_LOCATION = pwd + `cd #{pwd} && git rev-parse --git-dir 2>/dev/null`.chomp
4e75b7ba » Caged 2008-03-01 Initial import 27
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 28 class ApplicationController < OSX::NSObject
d4b7e48b » Caged 2008-03-04 Updated interface, now incl... 29 ib_outlet :commits_table
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 30 ib_outlet :commits_controller
d4b7e48b » Caged 2008-03-04 Updated interface, now incl... 31 ib_outlet :window
32 ib_outlet :main_canvas
33 ib_outlet :main_view
c5a3bf74 » benstiglitz 2008-03-12 Removed need for Core Anima... 34 ib_outlet :branch_field
1d06d2ce » Caged 2008-04-11 Implement an invisible tab ... 35 ib_outlet :tab_panel
36 ib_outlet :extras_segment
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 37 ib_outlet :local_branches_menu
38 ib_outlet :remote_branches_menu
39 ib_outlet :tags_menu
6040395d » Caged 2008-06-12 Add search UI with menu 40 ib_outlet :search_field
74cd84c5 » Caged 2008-06-13 Toggle controls when swappi... 41 ib_outlet :paging_segment
29509556 » Caged 2008-06-23 Fix refs menu so that it's ... 42 ib_outlet :branch_select
f74a0f36 » Caged 2008-03-03 Don't cause the task to wai... 43
44 def applicationDidFinishLaunching(sender)
4bfd4e9a » Caged 2008-03-09 Reset scroll position when ... 45 @window.makeKeyAndOrderFront(self)
f74a0f36 » Caged 2008-03-03 Don't cause the task to wai... 46 end
78d3665c » Caged 2008-03-03 Will now accept the pwd fro... 47
48 def applicationShouldTerminateAfterLastWindowClosed(notification)
49 return true
50 end
51
05a6b828 » Caged 2008-04-12 Testing out the quartz draw... Comment 52 def awakeFromNib
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 53 if repo
54 @window.delegate = self
55 column = @commits_table.tableColumns[0]
56 cell = CommitSummaryCell.alloc.init
57 column.dataCell = cell
d4b7e48b » Caged 2008-03-04 Updated interface, now incl... 58
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 59 @main_view.setFrameSize(@main_canvas.frame.size)
60 @main_canvas.addSubview(@main_view)
c5a3bf74 » benstiglitz 2008-03-12 Removed need for Core Anima... 61
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 62 @branch_field.cell.setBackgroundStyle(NSBackgroundStyleRaised)
74cd84c5 » Caged 2008-06-13 Toggle controls when swappi... 63 @tab_panel.setDelegate(self)
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 64
6040395d » Caged 2008-06-12 Add search UI with menu 65 setup_search_field
29509556 » Caged 2008-06-23 Fix refs menu so that it's ... 66 setup_refs_view_menu
74cd84c5 » Caged 2008-06-13 Toggle controls when swappi... 67
68 Notify.on "tab_view_changed" do |opts|
69 if(opts[:tab_item] != "commits")
70 @paging_segment.setEnabled(false)
71 @search_field.setEnabled(false)
72 else
73 @paging_segment.setEnabled(true)
74 @search_field.setEnabled(true)
75 end
76 end
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 77 end
4e75b7ba » Caged 2008-03-01 Initial import 78 end
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 79
05a6b828 » Caged 2008-04-12 Testing out the quartz draw... Comment 80 def repo
81 begin
82 @repo ||= Grit::Repo.new(REPOSITORY_LOCATION)
83 rescue Grit::InvalidGitRepositoryError
84 return false
85 end
86 end
87
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 88 ib_action :show_info_panel
89 def show_info_panel(sender)
b6c6d3b4 » kballard 2008-05-10 Why use @repo when we have ... 90 @info_controller ||= InfoWindowController.alloc.init_with_repository(repo)
f2c235a7 » Caged 2008-03-08 Implement colored diffs, in... 91 @info_controller.showWindow(self)
92 end
1d06d2ce » Caged 2008-04-11 Implement an invisible tab ... 93
94 ib_action :swap_tab
95 def swap_tab(segment)
05a6b828 » Caged 2008-04-12 Testing out the quartz draw... Comment 96 tag = %w(commits network)[segment.cell.tagForSegment(segment.selectedSegment)]
97 @tab_panel.selectTabViewItemWithIdentifier(tag)
1d06d2ce » Caged 2008-04-11 Implement an invisible tab ... 98 end
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 99
6040395d » Caged 2008-06-12 Add search UI with menu 100 def set_search_category(sender)
101 menu = @search_field.cell.searchMenuTemplate
102 menu.itemWithTitle(@current_search_item).setState(NSOffState)
103 menu.itemWithTitle(sender.title).setState(NSOnState)
104 @search_field.cell.setSearchMenuTemplate(menu)
105 @current_search_item = sender.title
106 end
107
8782859a » Caged 2008-06-13 Find commits by specifying ... 108 def search_commits(sender)
109 @commits_controller.search_commits(@current_search_item, sender.stringValue)
110 end
111
74cd84c5 » Caged 2008-06-13 Toggle controls when swappi... 112 def tabView_didSelectTabViewItem(tab_view, tab_item)
113 Notify.send "tab_view_changed", { :tab_item => tab_item.identifier }
114 end
8782859a » Caged 2008-06-13 Find commits by specifying ... 115
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 116 private
117 def setup_refs_view_menu
118 [@local_branches_menu, @remote_branches_menu, @tags_menu].each { |m| m.submenu.setAutoenablesItems(false) }
119
cb3fdfc7 » Caged 2008-08-22 Fixes #27: Launching from a... 120 heads = repo.heads.reject { |head| head.nil? }
121 heads = heads.sort_by do |head|
122 name = head.name rescue "temp head"
123 name == 'master' ? "***" : name
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 124 end
125
126 add_menu_item = lambda do |refs, menu|
127 refs.each_with_index do |head, index|
128 item = NSMenuItem.alloc.initWithTitle_action_keyEquivalent(head.name, :swap_branch, index.to_s)
129 item.setEnabled(true)
29509556 » Caged 2008-06-23 Fix refs menu so that it's ... 130 item.setTag(index)
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 131 item.setTarget(@commits_controller)
132 menu.submenu.addItem(item)
133 end
134 end
135
136 add_menu_item.call(heads, @local_branches_menu)
137 add_menu_item.call(repo.remotes, @remote_branches_menu)
138 add_menu_item.call(repo.tags, @tags_menu)
29509556 » Caged 2008-06-23 Fix refs menu so that it's ... 139
140 add_menu_item.call(heads, @branch_select.menu.itemAtIndex(0)) #local
141 add_menu_item.call(repo.remotes, @branch_select.menu.itemAtIndex(1)) #remote
142 add_menu_item.call(repo.tags, @branch_select.menu.itemAtIndex(2)) #tags
143
cb3fdfc7 » Caged 2008-08-22 Fixes #27: Launching from a... 144 current_head = repo.heads.first.name.to_sym rescue nil
29509556 » Caged 2008-06-23 Fix refs menu so that it's ... 145 item = @branch_select.itemAtIndex(0).submenu.itemWithTitle(current_head || :master)
146 @branch_select.cell.setMenuItem(item)
6040395d » Caged 2008-06-12 Add search UI with menu 147 end
148
149 def setup_search_field
150 @search_menu = NSMenu.alloc.initWithTitle("Search Menu")
151 @search_field.cell.setSearchMenuTemplate(@search_menu)
8782859a » Caged 2008-06-13 Find commits by specifying ... 152 @search_field.cell.setSendsWholeSearchString(true)
153 @search_field.setTarget(self)
154 @search_field.setAction(:search_commits)
6040395d » Caged 2008-06-12 Add search UI with menu 155 @search_menu.setAutoenablesItems(false)
156
157 add_menu_item = lambda do |title, tooltip, state|
158 item = NSMenuItem.alloc.initWithTitle_action_keyEquivalent(title, :set_search_category, "")
159 @search_menu.addItem(item)
160 item.setToolTip(tooltip)
161 item.setEnabled(true)
162 item.setTarget(self)
163 if state
164 item.setState(NSOnState)
165 @current_search_item = title
166 end
167 end
168
8782859a » Caged 2008-06-13 Find commits by specifying ... 169 add_menu_item.call("Message", "Search commit messages", true)
6040395d » Caged 2008-06-12 Add search UI with menu 170 add_menu_item.call("SHA1", "Find a commit by its SHA1 hash", false)
8782859a » Caged 2008-06-13 Find commits by specifying ... 171 add_menu_item.call("Committer", "Find all all commits by a particular committer", false)
6040395d » Caged 2008-06-12 Add search UI with menu 172 add_menu_item.call("Author", "Find all all commits by a particular author", false)
173 add_menu_item.call("Path", "Find commits based on a path", false)
8782859a » Caged 2008-06-13 Find commits by specifying ... 174 @search_field.cell.setPlaceholderString("Search commits...")
571feaa6 » Caged 2008-06-05 Add menu items allowing you... 175 end
4e75b7ba » Caged 2008-03-01 Initial import 176 end