-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbrowser.lua
More file actions
140 lines (122 loc) · 3.54 KB
/
browser.lua
File metadata and controls
140 lines (122 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
------------------------------------------------------------------------------
--
-- This file is part of the Corona game engine.
-- For overview and more information on licensing please refer to README.md
-- Home page: https://github.com/coronalabs/corona
-- Contact: support@coronalabs.com
--
------------------------------------------------------------------------------
local widget = require("widget")
local json = require("json")
Browser = {}
Browser.__index = Browser
function Browser:create(balance)
local brwsr = {} -- our new object
setmetatable(brwsr, Browser) -- make Browser handle lookup
brwsr.url = nil -- initialize our object
return brwsr
end
function Browser:show(x, y, width, height, url)
local toolbarWidth = 50
local iconSize = 40
local iconAlpha = 1.0
local mostRecentURL = nil
if self.browserGroup then
self.webview.isVisible = true
self.browserGroup.isVisible = true
else
self.browserGroup = display.newGroup()
self.webview = native.newWebView(x + (toolbarWidth/2), y, width - toolbarWidth, height)
self.browserGroup:insert(self.webview)
local background = display.newRect(x, y, width, height)
background:setFillColor(0.8, 0.8, 0.8)
self.browserGroup:insert(background)
local homeButton = widget.newButton
{
width = iconSize,
height = iconSize,
label = "",
defaultFile = "assets/browser_home.png",
overFile = "assets/browser_home.png",
onRelease = function()
self.webview:request(url)
end
}
homeButton.x = (x - (width/2)) + (homeButton.width / 2) + 5
homeButton.y = (y - (height/2)) + (homeButton.height / 2) + 20
homeButton.alpha = iconAlpha
self.browserGroup:insert(homeButton)
local backButton = widget.newButton
{
width = iconSize,
height = iconSize,
label = "",
defaultFile = "assets/browser_back.png",
overFile = "assets/browser_back.png",
onRelease = function()
self.webview:back()
end
}
backButton.x = homeButton.x
backButton.y = homeButton.y + backButton.height
backButton.alpha = iconAlpha
self.browserGroup:insert(backButton)
local forwardButton = widget.newButton
{
width = iconSize,
height = iconSize,
label = "",
defaultFile = "assets/browser_forward.png",
overFile = "assets/browser_forward.png",
onRelease = function()
self.webview:forward()
end
}
forwardButton.x = homeButton.x
forwardButton.y = backButton.y + forwardButton.height
forwardButton.alpha = iconAlpha
self.browserGroup:insert(forwardButton)
local browserButton = widget.newButton
{
width = iconSize,
height = iconSize,
label = "",
defaultFile = "assets/browser_external.png",
overFile = "assets/browser_external.png",
onRelease = function()
system.openURL(mostRecentURL or url)
end
}
browserButton.x = homeButton.x
browserButton.y = forwardButton.y + browserButton.height
browserButton.alpha = iconAlpha
self.browserGroup:insert(browserButton)
local function webListener(event)
mostRecentURL = event.url
if event.type == "loaded" then
if self.webview.canGoBack then
backButton.alpha = 1.0
else
backButton.alpha = 0.4
end
if self.webview.canGoForward then
forwardButton.alpha = 1.0
else
forwardButton.alpha = 0.4
end
end
end
self.webview:addEventListener( "urlRequest", webListener )
end
if self.url ~= url then
self.url = url
self.webview:request(url)
end
end
function Browser:hide()
if self.browserGroup then
self.webview.isVisible = false
self.browserGroup.isVisible = false
end
end
return Browser