Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard rewrite: select current layout on startup #196

Closed
wants to merge 125 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
bd2ba9e
Basic interface to select keyboard layout
jyeray Oct 1, 2017
c4e2d6f
load keyboard layouts returns a lists of keyboard layouts
jyeray Oct 3, 2017
8d89b7b
add test helper for keyboard rewrite tests
jyeray Oct 3, 2017
1fc2b83
extract explanatory method to test_helper
jyeray Oct 3, 2017
ab14abe
Layout selector load keyboard layouts
jyeray Oct 4, 2017
f2683eb
move method to test_helper
jyeray Oct 4, 2017
5a4f068
create keyboard layout class
jyeray Oct 4, 2017
f4663b1
load virtual console layouts instead xorg ones
jyeray Oct 6, 2017
a367d5c
initialize the layout description
jyeray Oct 6, 2017
490a5d4
does not return layouts that not have description
jyeray Oct 6, 2017
c759441
rename test
jyeray Oct 6, 2017
f5ccc28
Show description in layout list
jyeray Oct 8, 2017
b95aefc
Reorganize in another folder
jyeray Oct 8, 2017
3935d55
Fix some rubocop offenses
jyeray Oct 9, 2017
0599b60
Merge files
jyeray Oct 9, 2017
c883168
set the console layout
jyeray Oct 19, 2017
602d8cd
accept_handler closes the dialog
jyeray Oct 19, 2017
39b6906
accept_handler change the keymap to the selected layout
jyeray Oct 19, 2017
017fdb0
fix test
jyeray Oct 19, 2017
1a96ff6
Rename dialog module to ajust to YaST convention
jyeray Oct 22, 2017
5342771
add client
jyeray Oct 22, 2017
6af78ad
refactor: rename method
jyeray Oct 22, 2017
8145422
extract helpers module
jyeray Oct 22, 2017
fe64bac
fix can not import files
jyeray Oct 29, 2017
374937e
refactor: avoid to use block code in a .map
jyeray Oct 29, 2017
1156f51
fix typo error in test description
jyeray Oct 29, 2017
d3c10f9
inject keyboard layouts instead retrive in the dialog
jyeray Oct 29, 2017
1a60678
inject keyboard layouts in client
jyeray Oct 29, 2017
07ded50
Merge branch 'keyboard_rewrite' into change_keyboard_layout
jyeray Nov 1, 2017
6947786
fix tests
jyeray Nov 1, 2017
e51b382
Fix query widget when selecting a layout
jyeray Nov 5, 2017
33f8fa0
use keyboardLayout object instead of code to set layout
jyeray Nov 5, 2017
60ec6de
fix: match keyboard layout by description
jyeray Nov 5, 2017
e4c4c5a
remove unused require
jyeray Mar 6, 2018
ca6282d
set the keyboard layout also in x11
jyeray Mar 25, 2018
76e99ab
add textdomain
jyeray Mar 28, 2018
12a207f
rename Helpers module to KeyboardSpecHelper
jyeray Apr 2, 2018
cfa711b
rename directory
jyeray Apr 2, 2018
33d83f2
rename client
jyeray Apr 2, 2018
d6fa330
add technical debt file
jyeray Apr 2, 2018
91dcee9
Merge branch 'master' into keyboard_rewrite
jyeray Apr 2, 2018
159fbee
Merge branch 'keyboard_rewrite' into load_keyboard_layouts
jyeray Apr 2, 2018
6fe4b25
add textdomain
jyeray Apr 2, 2018
b3d62a6
Merge branch 'keyboard_rewrite' into change_keyboard_layout
jyeray Apr 2, 2018
000fd69
remove code from test_helper for coverage
jyeray Apr 3, 2018
51114a7
change keyboard rewrite test_helper to use the general test_helper fo…
jyeray Apr 3, 2018
d651109
remove unnecessary code from rewrite test_helper
jyeray Apr 3, 2018
52e67c6
Merge branch 'keyboard_rewrite' into change_keyboard_layout
jyeray Apr 3, 2018
a323902
Merge branch 'change_keyboard_layout' into test_layout
jyeray May 6, 2018
239be51
add UK layout
jyeray May 7, 2018
ed50c3e
.set_current_layout changes the current keyboard layout used in xorg
jyeray May 7, 2018
e466928
#layout_lists_handler change the keymap to the selected layout
jyeray May 7, 2018
1c837d5
add :notify to layout list
jyeray May 7, 2018
660d533
refactor: extract method
jyeray May 7, 2018
cb5c0b9
refactor: inline method
jyeray May 7, 2018
ecd9635
refactor: rename
jyeray May 7, 2018
918ebc0
#cancel_handler closes the dialog
jyeray May 12, 2018
4c35a89
closes keyboard restores the keyboard layout to the previous selected
jyeray May 12, 2018
3055101
rename method
jyeray May 12, 2018
c14c45e
allow use fit to filter tests to execute
jyeray May 12, 2018
1bd81c9
get_current_layout returns the current used keyboard layout
jyeray May 12, 2018
84be7a1
fix tests
jyeray May 12, 2018
e12f531
add spaces to output of command in test
jyeray May 12, 2018
21bf2a6
extract helper method
jyeray May 12, 2018
a43aee4
refactor: extract selecting_layout_from_list method
jyeray May 12, 2018
1e8b33f
refactor tests
jyeray May 12, 2018
edd7f48
refactor: extract methods
jyeray May 12, 2018
8f8e17c
remove unnecessary variable
jyeray May 12, 2018
a7c50bf
Add input to test selected layout
jyeray May 13, 2018
0883e14
add horizontal space
jyeray May 13, 2018
08431a2
in text mode do not try to changes the current keyboard layout in xorg
jyeray May 13, 2018
b2a2d54
add in X server describe
jyeray May 13, 2018
5c6dc12
in text mode changes the current keyboard layout in console
jyeray May 13, 2018
4a0e481
in X server do not try to change the current keyboard layout in console
jyeray May 13, 2018
e7b6fbc
extract common variable
jyeray May 13, 2018
8979cf5
use .map(&:strip)
jyeray May 13, 2018
555fa80
using ncurses inside X server do not raise error
jyeray May 14, 2018
e584845
refactor: extract method
jyeray May 14, 2018
2af85aa
Add comment for test
jyeray May 14, 2018
f78e8b6
when setting current keyboard layout in console log error info
jyeray May 15, 2018
9ceaddf
just some code style changes
jyeray May 15, 2018
c28ad8a
Merge branch 'master' into keyboard_rewrite
jyeray May 15, 2018
71c0a7d
Merge branch 'keyboard_rewrite' into change_keyboard_layout
jyeray May 15, 2018
a41ee51
Merge branch 'keyboard_rewrite' into test_layout
jyeray May 15, 2018
b51f3a2
Add .rubocop.yml
jyeray May 15, 2018
67eb794
Fix spec helper offenses
jyeray May 15, 2018
df76637
Fix spec offenses
jyeray May 15, 2018
80f6e65
remove redundant cop
jyeray May 15, 2018
6601ca1
Fix rubocop offenses
jyeray May 15, 2018
b2c5401
rename methods: do not use set and get in method names
jyeray May 15, 2018
673b138
Add top-level class documentation comment
jyeray May 15, 2018
132d702
Move rubocop.yml to root folder
jyeray May 16, 2018
60e6192
fix rubocop offense
jyeray May 16, 2018
44c0797
Extract SystemdStrategy
jyeray May 17, 2018
f1209b2
rename class
jyeray May 17, 2018
361a3d0
KeyboardLayout spec are now for SystemdStrategy
jyeray May 17, 2018
43de8f2
Remove Yast::Logger include
jyeray May 17, 2018
ec250f9
fix require
jyeray May 17, 2018
b6ae951
LayoutSelector now uses SystemdStrategy insted of KeyboardLayout
jyeray May 17, 2018
b80fad5
Keyboard client now uses SystemdStrategy insted of KeyboardLayout
jyeray May 17, 2018
3f50e6a
Remove unused methods
jyeray May 17, 2018
d4cae32
Create instance methods for SystemdStrategy
jyeray May 17, 2018
866a1e5
remove unnecessary require
jyeray May 17, 2018
428eeb5
Fix requires
jyeray May 17, 2018
3d8c145
Inject strategy to LayoutSelector class
jyeray May 17, 2018
c4940e0
Refactor: convert class methods into instace methods
jyeray May 17, 2018
4f2beda
Adjust require
jyeray May 17, 2018
65eb585
Update SystemdStrategy spec describes
jyeray May 17, 2018
737e695
Retrieve keyboard layouts from strategy
jyeray May 17, 2018
0602af9
lists the keyboard layouts
jyeray May 17, 2018
00d5191
LayoutSelector only receive an strategy
jyeray May 17, 2018
bceacc1
Add class top-documentation to SystemdStrategy
jyeray May 19, 2018
4dd9751
Apologize to rubocop
jyeray May 19, 2018
af9d949
Improve test: lists the keyboard layouts
jyeray May 19, 2018
94ad3ac
Create items to show layout in the list
jyeray May 19, 2018
0210b7e
Select the current layout in the list
jyeray May 19, 2018
e20046f
refactor: rename method
jyeray May 19, 2018
7464a15
Use layout code as item id instead of description
jyeray May 20, 2018
6346eb5
Fix tests: now QueryWidget to :layout_list returns the layout code
jyeray May 20, 2018
2688f68
Merge branch 'master' into keyboard_rewrite
jyeray Nov 22, 2018
ecf865a
Merge branch 'keyboard_rewrite' into change_keyboard_layout
jyeray Nov 22, 2018
25b3a9d
Merge branch 'change_keyboard_layout' into test_layout
jyeray Nov 22, 2018
73f84c2
Merge branch 'test_layout' into 4_add_rubocop
jyeray Nov 22, 2018
caf2ce3
Merge branch '4_add_rubocop' into 5_extract_repository
jyeray Nov 22, 2018
6e325c3
Merge branch '5_extract_repository' into 6_select_current_layout_on_s…
jyeray Nov 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# use the shared Yast defaults
inherit_from:
/usr/share/YaST2/data/devtools/data/rubocop_yast_style.yml

AllCops:
Exclude:
- 'keyboard/**/*'
- 'console/**/*'
- 'language/**/*'
- 'timezone/**/*'
- 'Rakefile'

7 changes: 7 additions & 0 deletions TECHNICALDEBT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**Keyboard rewrite tecnichal debt**
============================

**This file contains keep technical debt that should be fixed before finishing the project.**


- [ ] ```require_relative``` are being used temporary, for example in ```y2_keyboard/clients/keyboard.rb```, it must be replaced with ```require```.
3 changes: 3 additions & 0 deletions keyboard_rewrite/src/clients/keyboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "../lib/y2keyboard/clients/keyboard"

Y2Keyboard::Clients::Keyboard.run
14 changes: 14 additions & 0 deletions keyboard_rewrite/src/lib/y2keyboard/clients/keyboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative "../dialogs/layout_selector"
require_relative "../strategies/systemd_strategy"

module Y2Keyboard
module Clients
# Simple client to run LayoutSelector.
class Keyboard
def self.run
systemd_strategy = Y2Keyboard::Strategies::SystemdStrategy.new
Y2Keyboard::Dialogs::LayoutSelector.new(systemd_strategy).run
end
end
end
end
84 changes: 84 additions & 0 deletions keyboard_rewrite/src/lib/y2keyboard/dialogs/layout_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require "yast"
require "ui/dialog"

Yast.import "UI"
Yast.import "Popup"

module Y2Keyboard
module Dialogs
# Main dialog where the layouts are listed and can change the keyboard layout
class LayoutSelector < UI::Dialog
def initialize(strategy)
textdomain "country"
@keyboard_layouts = strategy.all
@previous_selected_layout = strategy.current_layout
@strategy = strategy
end

def dialog_options
Opt(:decorated, :defaultsize)
end

def dialog_content
VBox(
HBox(
HWeight(20, HStretch()),
HWeight(50, layout_selection_box),
HWeight(20, HStretch())
),
footer
)
end

def layout_selection_box
VBox(
SelectionBox(
Id(:layout_list),
Opt(:notify),
_("&Keyboard Layout"),
map_layout_items
),
InputField(Opt(:hstretch), _("&Test"))
)
end

def map_layout_items
@keyboard_layouts.map do |layout|
Item(
Id(layout.code),
layout.description,
layout.code == @previous_selected_layout.code
)
end
end

def accept_handler
@strategy.apply_layout(selected_layout)
finish_dialog
end

def cancel_handler
@strategy.load_layout(@previous_selected_layout)
finish_dialog
end

def layout_list_handler
@strategy.load_layout(selected_layout)
end

def selected_layout
selected = Yast::UI.QueryWidget(:layout_list, :CurrentItem)
@keyboard_layouts.find { |x| x.code == selected }
end

def footer
HBox(
HSpacing(),
PushButton(Id(:cancel), Yast::Label.CancelButton),
PushButton(Id(:accept), Yast::Label.AcceptButton),
HSpacing()
)
end
end
end
end
12 changes: 12 additions & 0 deletions keyboard_rewrite/src/lib/y2keyboard/keyboard_layout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Y2Keyboard
# This class represents a keyboard layout. Also have methods to interact with the system.
class KeyboardLayout
attr_reader :code
attr_reader :description

def initialize(code, description)
@code = code
@description = description
end
end
end
54 changes: 54 additions & 0 deletions keyboard_rewrite/src/lib/y2keyboard/strategies/systemd_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "cheetah"
require_relative "../keyboard_layout"

module Y2Keyboard
module Strategies
# Class to deal with systemd keyboard configuration
class SystemdStrategy
include Yast::Logger

LAYOUT_CODE_DESCRIPTIONS = {
"gb" => "English (UK)",
"es" => "Spanish",
"fr" => "French",
"us" => "English (US)"
}.freeze

def all
raw_layouts = Cheetah.run("localectl", "list-keymaps", stdout: :capture)
codes = raw_layouts.lines.map(&:strip)
codes_with_description = codes.select { |code| LAYOUT_CODE_DESCRIPTIONS.key?(code) }
codes_with_description.map { |x| KeyboardLayout.new(x, LAYOUT_CODE_DESCRIPTIONS[x]) }
end

def apply_layout(keyboard_layout)
Cheetah.run("localectl", "set-keymap", keyboard_layout.code)
end

def load_layout(keyboard_layout)
Cheetah.run("setxkbmap", keyboard_layout.code) if !Yast::UI.TextMode
begin
Cheetah.run("loadkeys", keyboard_layout.code) if Yast::UI.TextMode
rescue Cheetah::ExecutionFailed => e
log.info(e.message)
log.info("Error output: #{e.stderr}")
end
end

def current_layout
find_layout_with(current_layout_code)
end

def find_layout_with(code)
all.find { |x| x.code == code }
end

def current_layout_code
output = Cheetah.run("localectl", "status", stdout: :capture)
output.lines.map(&:strip).find { |x| x.start_with?("VC Keymap:") }.split.last
end

private :current_layout_code, :find_layout_with
end
end
end
62 changes: 62 additions & 0 deletions keyboard_rewrite/test/keyboard_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Spec helper for keyboard module tests
module KeyboardSpecHelper
def mock_ui_events(*events)
allow(Yast::UI).to receive(:UserInput).and_return(*events)
end

def given_layouts(layouts_to_return)
allow(Cheetah).to receive(:run).with(
"localectl", "list-keymaps", stdout: :capture
).and_return(layouts_to_return.join("\n"))
end

def given_a_current_layout(code)
allow(Cheetah).to receive(:run)
.with("localectl", "status", stdout: :capture)
.and_return(
" System Locale: LANG=en_US.UTF-8\n" \
" VC Keymap: #{code}\n" \
" X11 Layout: #{code}\n" \
" X11 Model: microsoftpro\n" \
" X11 Options: terminate:ctrl_alt_bksp\n"
)
end

def selecting_layout_from_list(layout)
allow(Yast::UI).to receive(:QueryWidget)
.with(:layout_list, :CurrentItem)
.and_return(layout.code)
end

def loadkeys_error
Cheetah::ExecutionFailed.new(
"loadkeys es",
"Execution of \"loadkeys es\" failed with status 1: " \
"Couldn't get a file descriptor referring to the console.",
"",
"Couldn't get a file descriptor referring to the console"
)
end

def expect_display_layouts(layouts)
allow(Yast::Term).to receive(:new).and_call_original
layouts.each do |layout|
expect(Yast::Term).to receive(:new).with(
:item,
Id(layout.code),
layout.description,
boolean
)
end
end

def expect_create_list_with_current_layout(layout)
allow(Yast::Term).to receive(:new).and_call_original
expect(Yast::Term).to receive(:new).with(
:item,
Id(layout.code),
layout.description,
true
)
end
end
106 changes: 106 additions & 0 deletions keyboard_rewrite/test/layout_selector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
require_relative "test_helper"
require "y2keyboard/keyboard_layout"
require "y2keyboard/dialogs/layout_selector"
require "y2keyboard/strategies/systemd_strategy"

describe Y2Keyboard::Dialogs::LayoutSelector do
english = Y2Keyboard::KeyboardLayout.new("en", "English")
spanish = Y2Keyboard::KeyboardLayout.new("es", "Spanish")
layouts = [english, spanish]
strategy = Y2Keyboard::Strategies::SystemdStrategy.new
subject(:layout_selector) { Y2Keyboard::Dialogs::LayoutSelector.new(strategy) }

before do
allow(Yast::UI).to receive(:OpenDialog).and_return(true)
allow(Yast::UI).to receive(:CloseDialog).and_return(true)
allow(strategy).to receive(:load_layout)
allow(strategy).to receive(:current_layout).and_return(english)
allow(strategy).to receive(:all).and_return(layouts)
end

describe "#run" do
before do
mock_ui_events(:cancel)
end

it "retrieve keyboard layouts from strategy" do
expect(strategy).to receive(:all)

layout_selector.run
end

it "lists the keyboard layouts" do
allow(strategy).to receive(:all).and_return(layouts)

expect_display_layouts(layouts)

layout_selector.run
end

it "select the current layout in the list" do
allow(strategy).to receive(:all).and_return(layouts)
allow(strategy).to receive(:current_layout).and_return(english)

expect_create_list_with_current_layout(english)

layout_selector.run
end
end

describe "#accept_handler" do
before do
mock_ui_events(:accept)
end

it "change the keymap to the selected layout" do
selecting_layout_from_list(spanish)

expect(strategy).to receive(:apply_layout).with(spanish)

layout_selector.run
end

it "closes the dialog" do
selecting_layout_from_list(spanish)
allow(strategy).to receive(:apply_layout)

expect(layout_selector).to receive(:finish_dialog).and_call_original

layout_selector.run
end
end

describe "#layout_list_handler" do
before do
mock_ui_events(:layout_list, :cancel)
end

it "change the keymap to the selected layout" do
selecting_layout_from_list(spanish)

expect(strategy).to receive(:load_layout).with(spanish)

layout_selector.run
end
end

describe "#cancel_handler" do
before do
mock_ui_events(:cancel)
end

it "closes the dialog" do
expect(layout_selector).to receive(:finish_dialog).and_call_original

layout_selector.run
end

it "restores the keyboard layout to the previous selected" do
allow(strategy).to receive(:current_layout).and_return(english)

expect(strategy).to receive(:load_layout).with(english)

layout_selector.run
end
end
end
Loading