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

Add !-postfix to modifying methods, prefer to extends Base methods. #232

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
134 changes: 97 additions & 37 deletions src/AtomShell/window.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using ..Blink
import Blink: js, id
import JSExpr: JSString, jsstring
import Base: position, size, close

export Window, flashframe, shell, progress, title,
centre, floating, loadurl, opentools, closetools, tools,
loadhtml, loadfile, css, front

# Keep sorted
export
center!,
loadfile!,
loadurl!,
opentools!,
position!,
progress!,
title!

mutable struct Window
id::Int
shell::Shell
Expand All @@ -20,7 +29,7 @@ end

Create and open a new Window through Electron.

If `async==false`, this function blocks until the Window is fully initialized
If `async=false`, this function blocks until the Window is fully initialized
and ready for you to communicate with it via javascript or the Blink API.

The `electron_options` dict is used to initialize the Electron window. See here
Expand Down Expand Up @@ -154,71 +163,116 @@ flashframe(win::Window, on = true) =
@dot_ win flashFrame($on)

"""
progress(win::Window, p=-1)
progress!(win::Window, p=-1)
NHDaly marked this conversation as resolved.
Show resolved Hide resolved

Sets progress value in progress bar. Valid range is [0, 1.0]. Remove progress
bar when progress < 0; Change to indeterminate mode when progress > 1.

https://github.com/electron/electron/blob/master/docs/api/browser-window.md#winsetprogressbarprogress-options
"""
progress(win::Window, p = -1) =
@dot_ win setProgressBar($p)
progress!(win::Window, p) = @dot_ win setProgressBar($p)
NHDaly marked this conversation as resolved.
Show resolved Hide resolved

# Deprecated
progress(win::Window, progress=-1) = progress!(win, progress)

"""
title(win::Window, title)
title!(window, title)

Set `win`'s title to `title.`
Set the window's title.
"""
title(win::Window, title) =
@dot_ win setTitle($title)
title!(win::Window, title) = @dot_ win setTitle($title)

# Deprecated
title(win::Window, title) = title!(win, title)

"""
title(win::Window)
title(window)

Get the window's title.
"""
title(win::Window) =
@dot win getTitle()
title(win::Window) = @dot win getTitle()

"""
center!(win::Window)

Center a window.
"""
center!(win::Window) = @dot_ win center()

# Deprecated (and misspelled?)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha 😛 Maybe this was written by a british english speaker? Perhaps Shashi? I know he wrote a lot of this originally, right?

centre(win::Window) =
@dot_ win center()

position(win::Window, x, y) =
@dot_ win setPosition($x, $y)
"""
position!(window, x, y)
position!(window, position)

Position a window.

This positions the top-left corner of the window to match the given coordinates.
"""
position!(w::Window, x, y) = @dot_ w setPosition($x, $y)
position!(w::Window, pos) = position!(w, pos...)

# Deprecated
position(w::Window, x, y) = position!(w, x, y)

"""
position(window)

Get the window's position.

This returns a tuple that represents the position of the top-left corner of the
window.
"""
function position(win::Window)
x, y = Int.(@dot win getPosition())
return (x, y)
end

position(win::Window) =
@dot win getPosition()
"""
resize!(window::$(repr(Window)), width, height)
resize!(window::$(repr(Window)), dims)

Resize a window to the given dimensions.
"""
Base.resize!(window::Window, width, height) = @dot_ window setSize($width, $height)
Base.resize!(window::Window, dims) = resize!(window, dims...)

size(win::Window, w::Integer, h::Integer) =
invoke(size, Tuple{Window, Any, Any}, win, w, h)
# Deprecated
Base.size(window::Window, width, height) = resize!(window, width, height)

size(win::Window, w, h) =
@dot_ win setSize($w, $h)
"""
size(window::$(repr(Window)))
NHDaly marked this conversation as resolved.
Show resolved Hide resolved

size(win::Window) =
@dot win getSize()
Return a tuple with the dimensions of the window.
"""
function Base.size(window::Window)
width, height = Int.(@dot window getSize())
return (width, height)
end

floating(win::Window, flag) =
@dot_ win setAlwaysOnTop($flag)

floating(win::Window) =
@dot win isAlwaysOnTop()

loadurl(win::Window, url) =
@dot win loadURL($url)
loadurl!(win::Window, url) = @dot win loadURL($url)
loadurl(win::Window, url) = loadurl!(win, url)
NHDaly marked this conversation as resolved.
Show resolved Hide resolved

loadfile(win::Window, f) =
loadurl(win, "file://$f")
loadfile!(win::Window, f) = loadurl(win, "file://$f")
loadfile(win::Window, f) = loadfile!(win, f)
NHDaly marked this conversation as resolved.
Show resolved Hide resolved

"""
opentools(win::Window)
opentools!(win::Window)

Open the Chrome Developer Tools on `win`.

See also: [`closetools`](@ref), [`tools`](@ref)
"""
opentools(win::Window) =
@dot win openDevTools()
opentools!(w::Window) = @dot win openDevTools()
opentools(w::Window) = opentools!(w)

"""
closetools(win::Window)
Expand All @@ -227,24 +281,29 @@ Close the Chrome Developer Tools on `win` if open.

See also: [`opentools`](@ref), [`tools`](@ref)
"""
closetools(win::Window) =
@dot win closeDevTools()
closetools!(win::Window) = @dot win closeDevTools()
closetools(win::Window) = closetools!(win)

"""
tools(win::Window)
tools!(win::Window)

Toggle the Chrome Developer Tools on `win`.

See also: [`opentools`](@ref), [`closetools`](@ref)
"""
tools(win::Window) =
@dot win toggleDevTools()
tools!(win::Window) = @dot win toggleDevTools()
tools(win::Window) = tools!(win)


front(win::Window) =
@dot win showInactive()

close(win::Window) =
@dot win close()
"""
close(window::$(repr(Window)))

Close a window.
"""
Base.close(win::Window) = @dot win close()

# Window content APIs

Expand All @@ -262,7 +321,7 @@ const initcss = """
<style>html,body{margin:0;padding:0;border:0;text-align:center;}</style>
"""

function loadhtml(win::Window, html::AbstractString)
function loadhtml!(win::Window, html::AbstractString)
tmp = string(tempname(), ".html")
open(tmp, "w") do io
println(io, initcss)
Expand All @@ -272,3 +331,4 @@ function loadhtml(win::Window, html::AbstractString)
@async (sleep(1); rm(tmp))
return
end
loadhtml(win::Window, html::AbstractString) = loadhtml!(win, html)
6 changes: 3 additions & 3 deletions test/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ using Test
# unknown reasons.
w = Window(Blink.@d(:show => false, :width=>150, :height=>100), async=false);
@testset "size Tests" begin
@test size(w) == [150,100]
@test size(w) == (150, 100)

size(w, 200,200)
@test size(w) == [200,200]
size(w, 200, 200)
@test size(w) == (200, 200)
end

# @testset "async" begin
Expand Down