Skip to content

Instantly share code, notes, and snippets.

@cho45
Last active August 29, 2015 14:05
Show Gist options
  • Save cho45/efba69e5c00b3f42a162 to your computer and use it in GitHub Desktop.
Save cho45/efba69e5c00b3f42a162 to your computer and use it in GitHub Desktop.
一度起動したあと、「システム環境設定」→「セキュリティとプライバシー」→「アクセシビリティ」で macruby を許可する必要あり
#!macruby
framework "Cocoa"
class MainView < NSView
def init
super
@log = ""
end
def drawRect(rect)
super
NSColor.clearColor.set
NSRectFill(bounds)
font = NSFont.boldSystemFontOfSize(24)
shadow = NSShadow.alloc.init
shadow.setShadowColor(NSColor.blackColor)
shadow.setShadowBlurRadius(2)
shadow.setShadowOffset([0, 0])
attrs = NSMutableDictionary.alloc.initWithDictionary({
NSForegroundColorAttributeName => NSColor.whiteColor,
NSFontAttributeName => font,
NSShadowAttributeName => shadow,
})
y = 0
@log.split(/\n/).reverse.each do |line|
storage = NSTextStorage.alloc.initWithString(line, attributes: attrs)
manager = NSLayoutManager.alloc.init
container = NSTextContainer.alloc.init
manager.addTextContainer(container)
storage.addLayoutManager(manager)
range = manager.glyphRangeForTextContainer(container)
10.times do
manager.drawGlyphsForGlyphRange(range, atPoint: [0, y])
end
rect = manager.boundingRectForGlyphRange([0, manager.numberOfGlyphs], inTextContainer: container)
y+= rect.size.height
end
end
def <<(log)
@log << log
@log = @log.split(/\n+/, -1).last(5).join("\n")
end
end
class AppDelegate
attr_accessor :window
def applicationDidFinishLaunching(a_notification)
prevKeyed = 0
NSEvent.addGlobalMonitorForEventsMatchingMask(NSKeyDownMask, handler: lambda {|e|
return unless e.type == NSKeyDown
mod = ""
if e.modifierFlags & NSShiftKeyMask != 0
mod += "⇧"
end
if e.modifierFlags & NSControlKeyMask != 0
mod += "⌃"
end
if e.modifierFlags & NSAlternateKeyMask != 0
mod += "⌥ "
end
if e.modifierFlags & NSCommandKeyMask != 0
mod += "⌘"
end
if e.modifierFlags & (NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask) == 0
char = readable(e.characters)
if Time.now.to_i - prevKeyed > 1
@view << "\n#{char}"
else
@view << char
end
else
@view << "\n#{mod}#{readable(e.charactersIgnoringModifiers).upcase}\n"
end
@view.needsDisplay = true
prevKeyed = Time.now.to_i
})
rect = [0, 0, 800, 500]
@window = NSWindow.alloc.initWithContentRect(rect, styleMask: NSBorderlessWindowMask, backing: NSBackingStoreBuffered, defer: 0)
@window.opaque = false
@window.hasShadow = false
@window.level = 1000
@window.movableByWindowBackground = true
# @window.ignoresMouseEvents = true
@window.makeKeyAndOrderFront(nil)
@window.orderFrontRegardless
@view = MainView.alloc.initWithFrame(rect)
@view.init
@view << "Initialized"
@window.contentView = @view
end
def readable(char)
p char
char.gsub(/[\r\e\x7f]/, {
"\r" => "↵\n",
"\e" => "⎋",
"\x7f" => "⌫",
})
end
end
app = NSApplication.sharedApplication
app.delegate = AppDelegate.new
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment