~chrisppy/titan

ref: 723d8c78cdfebe669e51d6e5396c78b05941aabc titan/browser.go -rw-r--r-- 3.0 KiB
723d8c78Chris Palmer set tab colors based on content and title colors 3 months ago
                                                                                
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
// This file is part of titan.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"strconv"

	"code.rocketnine.space/tslocum/cview"
)

func (a *app) loadBrowserPanel(links []string) {
	a.UI.BrowserPanel.Tabs = make(map[string]*tab)
	a.UI.BrowserPanel.TabNames = make([]string, 0)

	a.UI.BrowserPanel.TabbedPanels = cview.NewTabbedPanels()
	a.UI.BrowserPanel.TabbedPanels.SetTabBackgroundColor(a.Config.Theme.ContentBGColor)
	a.UI.BrowserPanel.TabbedPanels.SetTabTextColor(a.Config.Theme.ContentTextColor)
	a.UI.BrowserPanel.TabbedPanels.SetTabBackgroundColorFocused(a.Config.Theme.TitleBGColor)
	a.UI.BrowserPanel.TabbedPanels.SetTabTextColorFocused(a.Config.Theme.TitleFGColor)
	a.UI.BrowserPanel.TabbedPanels.SetTabSwitcherDivider("", "", "")

	a.UI.BrowserPanel.View = cview.NewFlex()
	a.UI.BrowserPanel.View.SetDirection(cview.FlexColumn)
	a.UI.BrowserPanel.View.AddItem(a.UI.BrowserPanel.TabbedPanels, 0, 1, false)

	if len(links) == 0 {
		links = []string{a.Config.HomePage}
	}

	for _, link := range links {
		t := tab{
			Content:      cview.NewTextView(),
			CurrentIndex: 0,
			Capsules:     make([]*capsule, 0),
		}
		t.Content.SetPadding(0, 0, 6, 6)
		t.Content.SetTextAlign(cview.AlignLeft)
		t.Content.SetScrollable(true)
		t.Content.SetWrap(true)
		t.Content.SetWordWrap(true)
		t.Content.SetDynamicColors(true)
		t.Content.SetBackgroundColor(a.Config.Theme.ContentBGColor)
		t.Content.SetTextColor(a.Config.Theme.ContentTextColor)
		t.Content.ScrollToBeginning()

		i := 0
		for {
			n := strconv.Itoa(i)
			if _, ok := a.UI.BrowserPanel.Tabs[n]; !ok {
				t.Name = n
				break
			}
			i++
		}

		c, err := a.getCapsule(link, "")
		if err != nil {
			a.Logger.Fatalf("unable to open link %s due to %s", link, err.Error())
			return
		}

		if c.Label == "" {
			c.Label = t.Name
		}
		t.Content.SetText(c.Text)

		t.Capsules = append(t.Capsules, c)

		a.UI.BrowserPanel.TabNames = append(a.UI.BrowserPanel.TabNames, t.Name)

		a.UI.BrowserPanel.TabbedPanels.AddTab(t.Name, c.Label, t.Content)
		a.UI.BrowserPanel.Tabs[t.Name] = &t
		a.UI.BrowserPanel.CurrentIndex = len(a.UI.BrowserPanel.TabNames) - 1
		a.UI.BrowserPanel.TabbedPanels.SetCurrentTab(a.UI.BrowserPanel.TabNames[a.UI.BrowserPanel.CurrentIndex])
	}
	a.UI.BrowserPanel.TabbedPanels.SetCurrentTab(a.UI.BrowserPanel.TabNames[a.UI.BrowserPanel.CurrentIndex])

	a.UI.Panels.AddPanel("browser", a.UI.BrowserPanel.View, true, true)
}