Skip to content

Commit

Permalink
Merge branch 'master' into ruby-warrior
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Dec 24, 2011
2 parents e8523aa + 25efa2e commit 33cb0a1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
46 changes: 46 additions & 0 deletions app/models/inverts_theme.rb
@@ -0,0 +1,46 @@
module InvertsTheme
def self.included(kls)
kls.extend(Config)
end
module Config
def default_theme(theme=nil)
theme ? @default_theme = theme : @default_theme
end
def alternate_theme(theme=nil)
theme ? @alternate_theme = theme : @alternate_theme
end
end

attr_accessor :current_theme
def current_theme
@current_theme ||= self.class.default_theme
end

def keyPressEvent(event)
invert_key = (event.key == Qt::Key_I && event.modifiers == Qt::CTRL)
invert_key ? invert_theme : super
end

protected

def invert_theme
self.current_theme = using_default_theme? ? alternate_theme : default_theme
set_theme(self.current_theme)
rescue NoMethodError
fail 'implement "set_theme(theme)" method.'
end

private

def using_default_theme?
current_theme == default_theme
end

def alternate_theme
self.class.alternate_theme
end

def default_theme
self.class.default_theme
end
end
23 changes: 19 additions & 4 deletions app/widgets/main.rb
@@ -1,11 +1,20 @@
require "qtwebkit"
require 'htmlentities'
require_relative '../models/inverts_theme.rb'

# this is the main view widget
class MainWidget < Qt::WebView
include InvertsTheme
default_theme "ace/theme/merbivore"
alternate_theme "ace/theme/clouds"

signals 'stdInRequested()'
slots 'rejectStdin()', 'acceptStdin()', 'evaluateRuby(QString)', 'setupQtBridge()', 'openRubyFile(const QString&)', 'saveRubyFile(const QString&)',
'QString gets()', 'alert(const QString&)', 'QString ask(const QString&)', 'append(const QString&)', 'appendError(const QString&)'

slots 'rejectStdin()', 'acceptStdin()', 'evaluateRuby(QString)',
'setupQtBridge()', 'openRubyFile(const QString&)',
'saveRubyFile(const QString&)', 'QString gets()',
'alert(const QString&)', 'QString ask(const QString&)',
'append(const QString&)', 'appendError(const QString&)'

def initialize
super
Expand All @@ -25,18 +34,24 @@ def initialize

Qt::Object.connect(@frame, SIGNAL("javaScriptWindowObjectCleared()"),
self, SLOT('setupQtBridge()'))

initialize_stdin_connection
self.load Qt::Url.new(File.expand_path(File.dirname(__FILE__) + "/../../public/index.html"))
show
end

protected

def set_theme(theme)
@frame.evaluateJavaScript("window.editor.setTheme('#{theme}')")
end

def version_description
'KidsRuby v' + KidsRuby::VERSION
end

def keyPressEvent(event)
return false if event.key == Qt::Key_Escape
notify_stdin_event_listeners(event) if @acceptStdin
super
end
Expand All @@ -50,8 +65,8 @@ def initialize_stdin_connection
end

def notify_stdin_event_listeners(event)
(@fr ||= FrameWriter.new(@frame)).keyPressEvent(event)
(@wr ||= RunnerWriter.new(@runner)).keyPressEvent(event)
(@frame_writer ||= FrameWriter.new(@frame)).keyPressEvent(event)
(@runner_writer ||= RunnerWriter.new(@runner)).keyPressEvent(event)
@stdInRejecter.keyPressEvent(event) if @stdInRejecter
end

Expand Down
27 changes: 27 additions & 0 deletions spec/models/inverts_theme_spec.rb
@@ -0,0 +1,27 @@
require_relative "../spec_helper"
require_relative "../../app/models/inverts_theme"

class DummyWidget
include InvertsTheme

default_theme "foo/bar"
alternate_theme "foo/baz"

def set_theme(theme)
# This method must be implemented in any class that.
# includes the InvertsTheme module.
end
end

describe 'dummy widget includes theme inversion' do
include KeyPressEventsTestHelper

describe 'inverting the theme' do
it 'delegates to the javascript function' do
widget = DummyWidget.new
widget.current_theme.must_equal "foo/bar"
widget.keyPressEvent(ctrl_i_key_press)
widget.current_theme.must_equal "foo/baz"
end
end
end
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -21,7 +21,11 @@ def return_key_press_event
key_press_event(Qt::Key_Return)
end

def key_press_event(key, text="")
Qt::KeyEvent.new(Qt::Event::KeyPress, key, Qt::NoModifier, text)
def ctrl_i_key_press
key_press_event(Qt::Key_I, '\t', Qt::CTRL)
end

def key_press_event(key, text='', modifier=Qt::NoModifier)
Qt::KeyEvent.new(Qt::Event::KeyPress, key, modifier, text)
end
end

0 comments on commit 33cb0a1

Please sign in to comment.