Skip to content

Commit

Permalink
Rename Modifiers.alt to altModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Oct 23, 2023
1 parent 1a9ccf1 commit ef56bc1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
54 changes: 26 additions & 28 deletions Sources/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,57 +96,55 @@ public struct Keys: RawRepresentable {
public static let f12 = Self(rawValue: "\u{E03C}")

/// Modifier keys are interpreted as toggles instead of key presses.
public enum Modifiers {
/// A special Keys value that causes all modifiers to be released.
public static let releaseAll = Keys(rawValue: "\u{E000}")

public static let shift = Keys(rawValue: "\u{E008}")
public static let control = Keys(rawValue: "\u{E009}")
public static let alt = Keys(rawValue: "\u{E00A}")
public static let meta = Keys(rawValue: "\u{E03D}")

public static var windows: Keys { meta }
public static var macCommand: Keys { meta }
public static var macOption: Keys { alt }
}
public static let shiftModifier = Keys(rawValue: "\u{E008}")
public static let controlModifier = Keys(rawValue: "\u{E009}")
public static let altModifier = Keys(rawValue: "\u{E00A}")
public static let metaModifier = Keys(rawValue: "\u{E03D}")

public static var windowsModifier: Keys { metaModifier }
public static var macCommandModifier: Keys { metaModifier }
public static var macOptionModifier: Keys { altModifier }

/// A special Keys value that causes all modifiers to be released.
public static let releaseModifiers = Keys(rawValue: "\u{E000}")
}

extension Keys {
/// Wraps a keys sequence with holding and releasing the shift key.
public static func shift(_ keys: Self) -> Self {
Self(rawValue: "\(Modifiers.shift.rawValue)\(keys.rawValue)\(Modifiers.shift.rawValue)")
Self(rawValue: "\(shiftModifier.rawValue)\(keys.rawValue)\(shiftModifier.rawValue)")
}

/// Wraps a keys sequence with holding and releasing the control key.
public static func control(_ keys: Self) -> Self {
Self(rawValue: "\(Modifiers.control.rawValue)\(keys.rawValue)\(Modifiers.control.rawValue)")
Self(rawValue: "\(controlModifier.rawValue)\(keys.rawValue)\(controlModifier.rawValue)")
}

/// Wraps a keys sequence with holding and releasing the alt key.
public static func alt(_ keys: Self) -> Self {
Self(rawValue: "\(Modifiers.alt.rawValue)\(keys.rawValue)\(Modifiers.alt.rawValue)")
Self(rawValue: "\(altModifier.rawValue)\(keys.rawValue)\(altModifier.rawValue)")
}

/// Wraps a keys sequence with holding and releasing the meta key.
public static func meta(_ keys: Self) -> Self {
Self(rawValue: "\(Modifiers.meta.rawValue)\(keys.rawValue)\(Modifiers.meta.rawValue)")
Self(rawValue: "\(metaModifier.rawValue)\(keys.rawValue)\(metaModifier.rawValue)")
}

/// Wraps a keys sequence with holding and releasing modifier keys.
public func combo(_ keys: Self, shift: Bool = false, control: Bool = false, alt: Bool = false, meta: Bool = false) -> Self {
public static func combo(_ keys: Self, shift: Bool = false, control: Bool = false, alt: Bool = false, meta: Bool = false) -> Self {
var rawValue = ""

if shift { rawValue += Modifiers.shift.rawValue }
if control { rawValue += Modifiers.control.rawValue }
if alt { rawValue += Modifiers.alt.rawValue }
if meta { rawValue += Modifiers.meta.rawValue }
if shift { rawValue += shiftModifier.rawValue }
if control { rawValue += controlModifier.rawValue }
if alt { rawValue += altModifier.rawValue }
if meta { rawValue += metaModifier.rawValue }

rawValue += keys.rawValue

if meta { rawValue += Modifiers.meta.rawValue }
if alt { rawValue += Modifiers.alt.rawValue }
if control { rawValue += Modifiers.control.rawValue }
if shift { rawValue += Modifiers.shift.rawValue }
if meta { rawValue += metaModifier.rawValue }
if alt { rawValue += altModifier.rawValue }
if control { rawValue += controlModifier.rawValue }
if shift { rawValue += shiftModifier.rawValue }

return Self(rawValue: rawValue)
}
Expand All @@ -172,7 +170,7 @@ extension Keys {
// Avoid sending it as a key event, which is dependent on keyboard layout.
// For example, the "q" key would type "a" on an AZERTY keyboard layout.
// Instead, use the alt+numpad code to type the character.
result += Modifiers.alt.rawValue
result += altModifier.rawValue
for digit in String(codePoint.value) {
switch digit {
case "0": result += Self.numpad0.rawValue
Expand All @@ -188,7 +186,7 @@ extension Keys {
default: fatalError()
}
}
result += Modifiers.alt.rawValue
result += altModifier.rawValue
}
else {
// Other printable characters will be sent as character events,
Expand Down
6 changes: 3 additions & 3 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public class Session {
return PollResult.success(elementIds)
}.value

return elementIds.map { $0.map { Element(in: self, id: $0) } }
return elementIds.map { $0.map { Element(session: self, id: $0) } }
}

/// Moves the pointer to a location relative to the current pointer position or an element.
Expand Down Expand Up @@ -289,15 +289,15 @@ public class Session {
/// - Parameter releaseModifiers: A boolean indicating whether to release modifier keys at the end of the sequence.
public func sendKeys(_ keys: [Keys], releaseModifiers: Bool = true) throws {
var value = keys.map { $0.rawValue }
if releaseModifiers { value.append(Keys.Modifiers.releaseAll.rawValue) }
if releaseModifiers { value.append(Keys.releaseModifiers.rawValue) }
try webDriver.send(Requests.SessionKeys(session: id, value: value))
}

/// Sends key presses to this session.
/// - Parameter keys: A key sequence according to the WebDriver spec.
/// - Parameter releaseModifiers: A boolean indicating whether to release modifier keys at the end of the sequence.
public func sendKeys(_ keys: Keys, releaseModifiers: Bool = true) throws {
let value = releaseModifiers ? [keys.rawValue, Keys.Modifiers.releaseAll.rawValue] : [keys.rawValue]
let value = releaseModifiers ? [keys.rawValue, Keys.releaseModifiers.rawValue] : [keys.rawValue]
try webDriver.send(Requests.SessionKeys(session: id, value: value))
}

Expand Down

0 comments on commit ef56bc1

Please sign in to comment.